Unix/Linux Go Back    

Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Pointer for 2D array seems to be 3D in C


2d array, address, memory, pointer

Thread Tools Search this Thread Display Modes
Old Unix and Linux 06-15-2017   -   Original Discussion by yifangt
yifangt's Unix or Linux Image
yifangt yifangt is offline VIP Member  
Join Date: Sep 2009
Last Activity: 23 May 2018, 3:33 PM EDT
Location: Saskatchewan, Canada
Posts: 530
Thanks: 345
Thanked 10 Times in 9 Posts
Pointer for 2D array seems to be 3D in C

I am struggling with the pointer to 2D-array (cf: 2D array of pointers). Can anybody help me elaborate how the pointer x moves in the memory to access the individual of y[2][6], especially the high lighted lines?
I have talked to one of the curators of the forum, but I am still not quite clear.
Here is my code:


int main(void)
    int (*x)[2][6];                 //pointer for integers array in size of 2x6 (2 rows x 6 columns),
                                    //.i.e the array is always with size of 12?
//    int (*a[8])[5];                 //Line 9: a is a pointer array of size 8, each for integer array of size 5 

    int y[2][6] = {{11,12,13,14,15,16},
                   {21,22,23,24,25,26}};    //2D array of integers
    int *z;                      //pointer to integer
    int i;

    z = y[0];
    for(i = 0;i<6;i++)
        printf("%d ",z[i]);

    x = &y;    // More complicated situation for me!
    x = y;     // Warning: incompatible pointer type.
//    x[0][0] = y[0][0];     // won't work
        printf("   (x[0][0]): %p\n",  x[0][0]);
        printf("  *(x[0][0]): %d\n",*(x[0][0]));        //Q1a
        printf("  x[0][0][0]: %d\n",x[0][0][0]);        //Q1b
        printf("*(x[0][0]+1): %d\n",*(x[0][0]+1));      //Q1c
//        printf("*(x[0][0]+2): %d\n",*(x[0][0]+2));
//        printf("*(x[0][0]+3): %d\n",*(x[0][0]+3));
//        printf("*(x[0][0]+4): %d\n",*(x[0][0]+4));
        printf("*(x[0][0]+5): %d\n",*(x[0][0]+5));

        printf("    x[0][1]: %p\n",   x[0][1]);        //Q2a
        printf("    *x[0][1]: %d\n",*(x[0][1]));
        printf("  x[0][1][0]: %d\n",x[0][1][0]);       //Q2b 
        printf("*(x[0][1]+1): %d\n",*(x[0][1]+1));     //Q2c 
//        printf("*(x[0][1]+2): %d\n",*(x[0][1]+2));
//        printf("*(x[0][1]+3): %d\n",*(x[0][1]+3));
        printf("*(x[0][1]+4): %d\n",*(x[0][1]+4));
        printf("*(x[0][1][4]): %d\n",*(x[0][4]));
    printf("&y: %p\n", &y);
    printf(" y: %p\n",  y);
    printf(" x: %p\n",  x);
    printf("&x: %p\n", &x);
    return 0;

1) Although y and &y are the same, but x = y issues warning;
2) Q1a/Q2a is the part I think I understand which is the first element of each row of y.
3) but Q1b/c, and Q2b/c turns out to be 3-D to me.
Can anybody give me a diagram how pointer x moves in the memory for each member of y?
4) Line 9: int (*a[8])[5]; is related, and I put it here for future reference but skip it at this moment.

Thanks a lot!
Sponsored Links
Old Unix and Linux 06-16-2017   -   Original Discussion by yifangt
Corona688's Unix or Linux Image
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
Join Date: Aug 2005
Last Activity: 23 May 2018, 6:42 PM EDT
Location: Saskatchewan
Posts: 22,677
Thanks: 1,178
Thanked 4,324 Times in 3,987 Posts
1) They probably differ in being constants or non-constants.

3) It's "3d" because you have an extra pointer in the way. Every level of pointers requires another dereferencing operation to access it. One level pointer requires one. Two level pointer requires two. Three level pointer requires three. 97 level pointer requires 97. That is the pattern.

The first dereferencing operation gets rid of is x itself; once you do so, you are essentially accessing y. The next level of pointers points to individual rows inside y. The last level gets you an individual cell in that row.

x; // pointer to Y
(*x); // Y, i.e. pointer to rows
(*x)[0]; // Pointer to row 0
(*x)[0][0] // Row 0 col 0.  No longer a pointer.

The diagram is x -> y -> row -> column.
The Following 2 Users Say Thank You to Corona688 For This Useful Post:
jim mcnamara (06-18-2017), yifangt (07-11-2017)
Sponsored Links

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Unclear pointer and array yifangt Programming 29 01-20-2014 01:47 PM
structure pointer array as function parameters mscoder Programming 2 06-21-2011 04:10 PM
help with char pointer array in C omega666 Programming 1 03-30-2011 10:48 AM
C pointer/array duality confusion gorga Programming 4 06-07-2010 05:18 PM
Storing pointer array in C arunkumar_mca UNIX for Dummies Questions & Answers 1 08-23-2007 08:49 AM

All times are GMT -4. The time now is 02:18 AM.