ryvnf ryvnf - 1 year ago 90
C Question

Does "int (*)[]" decay into "int **" in a function parameter?

I posted this question on programmers.stackexchange earlier today. I have always assumed that

int (*)[]
does not decay into
int **
in function parameters but I got multiple responses to my question that suggested that it does.

I have used
int (*)[]
heavily in my function parameters but now I have become really confused.

When I compile this function using
gcc -std=c99 -pedantic -Wall

void function(int (*a)[])

I get this error message:

c99 -Wall -pedantic -c main.c -o main.o
main.c: In function ‘function’:
main.c:3:11: error: invalid application of ‘sizeof’ to incomplete type ‘int[]’
make: *** [main.o] Error 1

Which suggests that
has the type
int []
and not
int *

Can someone explain if things like
int (*)[]
decays into
int **
in function parameters and give me some reference (from the standard documents perhaps) that proves why it is so.

Answer Source

Only array types converted to pointer to its first element when passed to a function. a is of type pointer to an array of int, i.e, it is of pointer type and therefore no conversion.

For the prototype

void foo(int a[][10]);

compiler interpret it as

void foo(int (*a)[10]);  

that's because a[] is of array type. int a[][10] will never be converted to int **a. That said, the second para in that answer is wrong and misleading.

As a function parameter, int *a[] is equivalent to int ** this is because a is of array type .

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download