Reputation: 92864
An array type decays to a pointer type when it is passed to a function
That means in
int func(int x[*p])
*p
should not be evaluated as the declaration is equivalent to int func(int *x)
Does the same hold for pointer to arrays?
Here is the code
int *p=0;
void func(int (*ptr)[*p]) //A
{
// code
}
int main()
{
int arr[5][5];
func(arr);
}
Is evaluation of *p
at //A
guaranteed by the Standard?
I tried with and without optimization on g++4.6. With optimizations enabled I don't get segfault. On clang the code is not giving any segfault even without any optimizations.
Upvotes: 2
Views: 159
Reputation: 6266
From the C99 Standard Section 6.7.5.2 Array declarators, paragraph 1:
- In addition to optional type qualifiers and the keyword static, the [ and ] may delimit an expression or *. If they delimit an expression (which specifies the size of an array), the expression shall have an integer type. If the expression is a constant expression, it shall have a value greater than zero. The element type shall not be an incomplete or function type. The optional type qualifiers and the keyword static shall appear only in a declaration of a function parameter with an array type, and then only in the outermost array type derivation.
The expression *p
evaluates to 0 and does not satisfy the requirements of the above paragraph, so the behavior is undefined.
Upvotes: 2