CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Jun 2017
    Posts
    13

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2017
    Posts
    13

    Thumbs up Re: How to type_cast two dimensional array using pointer?

    Quote Originally Posted by laserlight View Post
    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

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: How to type_cast two dimensional array using pointer?

    Note that
    Code:
    int (*GM)[10];
    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
    Code:
    int *GM[10]
    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)

  5. #5
    Join Date
    Feb 2017
    Posts
    677

    Re: How to type_cast two dimensional array using pointer?

    Quote Originally Posted by Thaus View Post
    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.

  6. #6
    Join Date
    Jun 2017
    Posts
    13

    Re: How to type_cast two dimensional array using pointer?

    Quote Originally Posted by wolle View Post
    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.

  7. #7
    Join Date
    Feb 2017
    Posts
    677

    Re: How to type_cast two dimensional array using pointer?

    Quote Originally Posted by Thaus View Post
    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.

  8. #8
    Join Date
    Jun 2017
    Posts
    13

    Re: How to type_cast two dimensional array using pointer?

    Quote Originally Posted by wolle View Post
    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

  9. #9
    Join Date
    Feb 2017
    Posts
    677

    Re: How to type_cast two dimensional array using pointer?

    Quote Originally Posted by Thaus View Post
    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.

  10. #10
    Join Date
    Jun 2017
    Posts
    13

    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 ?

  11. #11
    Join Date
    Jun 2017
    Posts
    13

    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;
        }

  12. #12
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  13. #13
    Join Date
    Jun 2017
    Posts
    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");
     	}
    Name:  SDK.jpg
Views: 341
Size:  27.1 KB

  14. #14
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  15. #15
    Join Date
    Jun 2017
    Posts
    13

    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
  •  





Click Here to Expand Forum to Full Width

Featured