简体   繁体   中英

2D Array and pointer to a pointer

Question from a past exam paper:

"Which of the following:

int a[4][4], (*b)[4], *c[4], **d;

Could you pass into a function expecting a pointer to a pointer to an int ie

int funct(int **);

Explain your answer."

The answer is c and di believe?

I am just struggling to understand why the rest arn't allowed?

Any help would be appreciated.

funct expects an int ** - that is, a pointer-to-a-pointer-to-an-int. d is literally that, so no problem. c works too, since it's an array of pointers-to-int, which will therefore decay into a pointer-to-a-pointer-to-an-int when used in a function call context.

a and b won't work, since they're not compatible types. a is an array-of-arrays-of-int, and b is aa pointer-to-array-of-int. a will decay into a pointer-to-array-of-int when passed as a parameter.

If I can add some information on the difference between the above answers.

This - [] has higher precedence than this * when declaring an array. That combined with the assumption that arrays and pointers are always interchangeable (they are not) catches a lot of people out.

Naively one would assume that int *arr[] would be a pointer to an array because read from left to right this is a natural assumption.

However it is actually an array of integer pointers. You want a pointer to an array? Tell C so:

int (*arr)[array_size];

by setting your own precedence.

As Carl has explained only answers c and d are applicable because in the d case you already have a pointer to a pointer and in the c case the array of pointers will decay to a pointer to a pointer when passed as an argument to a function.

If you want a function that will read a and b , the signature needs to change to the following:

int funct(int (*array_name)[array_size]); 

Note that in the above here you will need to specify the size of the array that the pointer will point to.

Some good answers on two dimensional arrays and pointers to pointers here and here .

in general if you pass an array to a function it is passed as pointer to first element of the array ...

  1. int a[4][4] to a function ie fun(a); it is passed as int (*p)[4] so fun parameter must be fun(int (*p)[])
    here first element itself is an array of 4 integers
    fun(a) ---> fun(int (*p)[])

  2. int (*b)[4] is a pointer to array of 4 integers so calling fun(b) requires parameter must be fun(int (*pt)[])
    fun(b) ---> fun(int (*pt)[])

  3. int *c[4] is an array of 4 integer pointers ..so calling fun(c) requires parameter must be pointer to first element of array. here first element is itself an int pointer..so fun(int **p)
    fun(c) ---> fun(int **p)

  4. int **d is double pointer to an int..so calling fun(d) requires parameter must be fun(int **p)
    fun(d) ---> fun(int **p)

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