How to type_cast two dimensional array using pointer?
I want to know whether the typecasting which i did is right or wrong. Through Internet search, i have found out but i am not sure. Please teach and correct me?
Code:
int Generator_matrix[10][10];
int (*GM)[10]= Generator_matrix;//Typecasting
int values[10][10];
int (*outdata)[10]= values;//Typecasting
Re: How to type_cast two dimensional array using pointer?
It is not a typecast, but rather an implicit conversion. In most contexts, an array is converted to a pointer to its first element, hence an array of ten arrays of ten int would be converted to a pointer to an array of ten int. Both GM and outdata are pointers to an array of ten ints, so the implicit conversion is correct.
Re: How to type_cast two dimensional array using pointer?
Quote:
Originally Posted by
laserlight
It is not a typecast, but rather an implicit conversion. In most contexts, an array is converted to a pointer to its first element, hence an array of ten arrays of ten int would be converted to a pointer to an array of ten int. Both GM and outdata are pointers to an array of ten ints, so the implicit conversion is correct.
Thank you for your explanation
Re: How to type_cast two dimensional array using pointer?
Note that
where GM is a pointer to an array of 10 ints as in laserlight's post - and hence can be initialised to another pointer in int.
But
where GM is an array of 10 pointers to int.
These two are not the same but can be confused.
Re: How to type_cast two dimensional array using pointer?
Quote:
Originally Posted by
Thaus
I want to know whether the typecasting which i did is right or wrong. Through Internet search, i have found out but i am not sure. Please teach and correct me?
Code:
int Generator_matrix[10][10];
int (*GM)[10]= Generator_matrix;//Typecasting
int values[10][10];
int (*outdata)[10]= values;//Typecasting
This is an example of a typecast that could be relevant here,
Code:
int* GM = (int*)Generator_matrix; // typecast to int*
//
GM[42] = 13; // access using array syntax
GM is now a pointer to int and can be thought of as an array GM[100] that is overlaid on Generator_matrix[10][10].
Re: How to type_cast two dimensional array using pointer?
Quote:
Originally Posted by
wolle
This is an example of a typecast that could be relevant here,
Code:
int* GM = (int*)Generator_matrix; // typecast to int*
//
GM[42] = 13; // access using array syntax
GM is now a pointer to int and can be thought of as an array GM[100] that is overlaid on Generator_matrix[10][10].
@wolle
Thanks for your response. As per your note,
Code:
int *GM = (int*)(Generator_matrix) // typecast to int* , GM is now a pointer to int.
Actually, Generator_matrix is two-dimensional array i.e. Generator_matrix[10][10]. Then how pointer points to two-dimensional array. Not clear.
Re: How to type_cast two dimensional array using pointer?
Quote:
Originally Posted by
Thaus
Actually, Generator_matrix is two-dimensional array i.e. Generator_matrix[10][10]. Then how pointer points to two-dimensional array. Not clear.
It's because arrays, one-dimensional as well as multi-dimensional, are laid out linearly in memory one element after the other in a certain known order specified by the C++ standard.
The name of an array is really just a pointer to the first element. It means this will also work for example,
Code:
int* GM = &Generator_matrix[0][0]; // GM is pointing at the first element of Generator_matrix.
The above assigns the address of the first element of Generator_matrix to GM which then can be viewed as an array GM[100] that is laid out on top of the array Generator_matrix[10][10] (just like with the type-cast version in my post #5).
Re: How to type_cast two dimensional array using pointer?
Quote:
Originally Posted by
wolle
It's because arrays, one-dimensional as well as multi-dimensional, are laid out linearly in memory one element after the other in a certain known order specified by the C++ standard.
The name of an array is really just a pointer to the first element. It means this will also work for example,
Code:
int* GM = &Generator_matrix[0][0]; // GM is now pointing at the first element of Generator_matrix.
The above assigns the address of the first element of Generator_matrix to GM which is a pointer to int and now holds this address.
Now, i am clear. Thanks
Re: How to type_cast two dimensional array using pointer?
Quote:
Originally Posted by
Thaus
Now, i am clear. Thanks
Note that this is low-level stuff that is handy on occasion but something you shouldn't do all the time in general code.
Re: How to type_cast two dimensional array using pointer?
I tried with simple example. I need to print H_Matrix value but instead its printing
enter the matrix
-2064509760-2064509736-2064509712-2064509688-2064509664-2064509640
-2064509688-2064509664-2064509640-2064509616-2064509592-2064509568
-2064509616-2064509592-2064509568-2064509544-2064509520-2064509496
Code:
#include <stdio.h>
int main()
{
// coding for the general form beginning using rows and cols
int i,j;
int H_Matrix[3][6]={{1,1,0,0,1,0},{1,0,0,1,0,1},{1,1,1,0,0,1}};
int (*GM)[3][6]=&H_Matrix;
printf("enter the matrix");
for(i=0;i<3;i++)
{
for(j=0;j<6;j++)
{
printf("%d",*(*(GM+ i) + j));
}
printf("\n");
}
return 0;
}
What wrong ?
Re: How to type_cast two dimensional array using pointer?
Now its working
int main()
Code:
{
// coding for the general form beginning using rows and cols
int i,j;
int H_Matrix[3][6]={{1,1,0,0,1,0},{1,0,0,1,0,1},{1,1,1,0,0,1}};
int (*GM)[6]=H_Matrix;
printf("enter the matrix");
for(i=0;i<3;i++)
{
for(j=0;j<6;j++)
{
printf("%d",*(*(GM+ i) + j));
}
printf("\n");
}
return 0;
}
Re: How to type_cast two dimensional array using pointer?
Now, GM is a pointer to an array of 3 arrays of 6 ints. That is, the expression (GM + i) when i == 1 results in a pointer to the next array of 3 arrays of 6 ints... but you only have one such array! If you had an array of N arrays of 3 arrays of 6 ints, then having GM be a pointer to an array of 3 arrays of 6 ints would make more sense.
1 Attachment(s)
Re: How to type_cast two dimensional array using pointer?
I am working with XSDK for hardware implementation but all deals with C/C++.
Expected Result: [0 1 1 1 0 0
1 1 0 0 1 0
1 1 1 0 0 1 ]
But if you notice tera console, its showing incorrect answer. I don`t offset and pointer declaration is right or wrong?
Code:
int main()
{
init_platform();
//u32 *Results;
int i,j,k,m;
int Generator_matrix[3][6];
int (*GM)[6]=Generator_matrix;
XLdpc_encoding LDPC;
XLdpc_encoding *LDPCPTR=&LDPC;
XLdpc_encoding_Initialize(LDPCPTR,XPAR_LDPC_ENCODING_0_DEVICE_ID);
print("\r\n---FOR HW---\r\n");
XLdpc_encoding_EnableAutoRestart(LDPCPTR);
XLdpc_encoding_Start(LDPCPTR);
while (!XLdpc_encoding_IsDone(LDPCPTR));
for(i=0;i<3;i++)
{
for(j=0;j<6;j++)
{
//while (!XLdpc_encoding_IsDone(LDPCPTR));
XLdpc_encoding_Read_Generator_Words(LDPCPTR, 0, GM, 6); /// XLdpc_encoding_Read_Generator_Words(LDPCPTR, int offset, int *data, int length);
xil_printf("%d\t", *(*(GM+ i) + j));
}
printf("\n");
}
Attachment 34889
Re: How to type_cast two dimensional array using pointer?
You should indent your code properly, e.g.,
Code:
int main()
{
init_platform();
//u32 *Results;
int i,j,k,m;
int Generator_matrix[3][6];
int (*GM)[6] = Generator_matrix;
XLdpc_encoding LDPC;
XLdpc_encoding *LDPCPTR=&LDPC;
XLdpc_encoding_Initialize(LDPCPTR,XPAR_LDPC_ENCODING_0_DEVICE_ID);
print("\r\n---FOR HW---\r\n");
XLdpc_encoding_EnableAutoRestart(LDPCPTR);
XLdpc_encoding_Start(LDPCPTR);
while (!XLdpc_encoding_IsDone(LDPCPTR));
for (i = 0; i < 3; i++)
{
for (j = 0; j < 6; j++)
{
//while (!XLdpc_encoding_IsDone(LDPCPTR));
XLdpc_encoding_Read_Generator_Words(LDPCPTR, 0, GM, 6); /// XLdpc_encoding_Read_Generator_Words(LDPCPTR, int offset, int *data, int length);
xil_printf("%d\t", *(*(GM + i) + j));
}
printf("\n");
}
Next, change this:
Code:
XLdpc_encoding_Read_Generator_Words(LDPCPTR, 0, GM, 6);
xil_printf("%d\t", *(*(GM + i) + j));
to:
Code:
XLdpc_encoding_Read_Generator_Words(LDPCPTR, 0, Generator_matrix, 6);
xil_printf("%d\t", Generator_matrix[i][j]);
Do you get the expected output? If not, then the error may lie with populating Generator_matrix.
Re: How to type_cast two dimensional array using pointer?
No, i didn`t get expected result. Let`s will try and update you.