-
June 19th, 2017, 07:11 AM
#1
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
-
June 19th, 2017, 07:15 AM
#2
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.
-
June 19th, 2017, 07:18 AM
#3
Re: How to type_cast two dimensional array using pointer?
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
-
June 19th, 2017, 07:38 AM
#4
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
June 19th, 2017, 02:40 PM
#5
Re: How to type_cast two dimensional array using pointer?
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].
Last edited by wolle; June 19th, 2017 at 03:18 PM.
-
June 19th, 2017, 11:08 PM
#6
Re: How to type_cast two dimensional array using pointer?
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.
Last edited by 2kaud; June 20th, 2017 at 01:51 AM.
-
June 20th, 2017, 12:14 AM
#7
Re: How to type_cast two dimensional array using pointer?
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).
Last edited by wolle; June 20th, 2017 at 12:27 AM.
-
June 20th, 2017, 12:16 AM
#8
Re: How to type_cast two dimensional array using pointer?
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
-
June 20th, 2017, 12:31 AM
#9
Re: How to type_cast two dimensional array using pointer?
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.
-
June 20th, 2017, 12:48 AM
#10
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 ?
-
June 20th, 2017, 12:57 AM
#11
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;
}
-
June 20th, 2017, 12:58 AM
#12
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.
-
June 20th, 2017, 01:10 AM
#13
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");
}
-
June 20th, 2017, 01:33 AM
#14
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.
-
June 20th, 2017, 01:43 AM
#15
Re: How to type_cast two dimensional array using pointer?
No, i didn`t get expected result. Let`s will try and update you.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|