I wrote a function which takes a pointer to an array to initialize its values:
#define FIXED_SIZE 256
int Foo(int *pArray[FIXED_SIZE])
{
/*...*/
}
//Call:
int array[FIXED_SIZE];
Foo(&array);
And it doesn’t compile:
error C2664: ‘Foo’ : cannot convert parameter 1 from ‘int (*__w64 )[256]’ to ‘int *[]’
However, I hacked this together:
typedef int FixedArray[FIXED_SIZE];
int Foo(FixedArray *pArray)
{
/*...*/
}
//Call:
FixedArray array;
Foo(&array);
And it works. What am I missing in the first definition? I thought the two would be equivalent…
In the first case,
pArrayis an array of pointers, not a pointer to an array.You need parentheses to use a pointer to an array:
You get this for free with the
typedef(since it’s already a type, the*has a different meaning). Put differently, thetypedefsort of comes with its own parentheses.Note: experience shows that in 99% of the cases where someone uses a pointer to an array, they could and should actually just use a pointer to the first element.