简体   繁体   中英

Pointer to 2D Array

I'm given the following snippet of code:

int group1[3][3] = {3,4,5,1,9,8};
int group2[3][3] = {{1},{2,3},{4,5}};
int *gPtr1 = group1;
int *gPtr2 = group2;

The question asks me for the value of: (I'm supposed to answer this on paper, of course, no computer)

*(gPtr1 + 3)
*(gPtr2 + 3)

Normally, I know for 1-dimensional array it adds an "address" instead of value. So for example:

int  balance[10]={1,2,6,4};
int *p=balance;  //p points to balance [0]

p+=3;           //points to balance [3]

However, I cant seem to make a pointer to a 2D array to test this out as in the question, I always get an "Initialization from incompatible pointer type" error.

The correct records will look like

int ( *gPtr1 )[3] = group1;
int ( *gPtr2 )[3] = group2;

And these expressions

*(gPtr1 + 3)
*(gPtr2 + 3)

are trying to access the memory after the last elements of the arrays because the arrays have only three rows. That is the type of expression *(gPtr1 + 3) is int[3] and there are only three such elements in the oridinal arrays.

or you could write

int *gPtr1 = ( int * )group1;
int *gPtr2 = ( int * )group2;

In this case using the pointers the arrays are interpretated as one-dimensional arrays with 9 elements and expressions

*(gPtr1 + 3)
*(gPtr2 + 3)

will return correspondingly

1
2

To access the address of group1 pass it to the pointer in this way:

int *gPtr1 = &group1[0][0];

Using that it's easy to find the soultions: 1 and 2.

Maybe this will help you get started:

#include <stdio.h>

int group1[3][3] = {3,4,5,1,9,8};
int group2[3][3] = {{1},{2,3},{4,5}};

int main( void )
{
    int *p;
    int i;
    for (p = group1[0], i = 0; i < 9; ++i, ++p )
        printf( "%d - ", *p);
    printf( "\n" );
    for (p = group2[0], i = 0; i < 9; ++i, ++p )
        printf( "%d - ", *p);
    printf( "\n" );
    return 0;
}

If you understand how memory in that 2D array is stored, it should be clear why this will work. Since I assume that understanding is what you're looking for, I'll leave you to figure that part out on your own. :-)

Two-dimensional matrices are represented in row-major order in memory. This is because memory is one-dimensional, and so it stores two-dimensional arrays as a concatenated list of rows.

When you create group1, you get an array of integers that is 9 long, and it looks like:

3 4 5 1 9 8 _ _ _

So, when you do *(gPtr1 + 3) , it adds 3 to the original location of gPtr1 , which is the address of 3 . Adding 3 gets you to the location of 1 , which you dereference.

So, when you create group2, you get an array of integers that is 9 long, and it looks like:

1 _ _ 2 3 _ 4 5 _

So, when you do *(gPtr2 + 3) , it adds 3 to the original location of gPtr2 , which is the address of 1 . Adding 3 gets you to the location of 2 , which you dereference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM