1

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 d i believe?

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

Any help would be appreciated.

user2469515
  • 373
  • 3
  • 12

3 Answers3

1

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 a a pointer-to-array-of-int. a will decay into a pointer-to-array-of-int when passed as a parameter.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • Yes, that's correct. It's a pointer already - only array types pull that trick. – Carl Norum Jun 27 '13 at 05:20
  • @user2469515 Decay isn't really the relevant issue. The thing that b points to is an array of 4 ints, whereas what is wanted is something that points to an `int*`. b+1 is `4*sizeof(int)` bytes greater than b, whereas what is wanted something for which the difference is `sizeof(int*)` bytes. – Jim Balter Jun 27 '13 at 05:29
  • @CarlNorum: Arrays are not the only type that are automatically converted to pointers. – Eric Postpischil Jun 27 '13 at 10:39
  • It's true - functions do it too. I guess I just meant in the context of the OP's question, where there are only arrays. – Carl Norum Jun 27 '13 at 14:50
0

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.

Community
  • 1
  • 1
Nobilis
  • 7,310
  • 1
  • 33
  • 67
0

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 i.e. 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)