2

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.

3
  • Isn't declaring void func(int (*ptr)[*p]) exactly the same as void func(int *ptr[])? Commented Feb 27, 2012 at 19:52
  • 3
    I had no idea that void func(int x[RANDOM_STUFF]) was valid! Commented Feb 27, 2012 at 20:57
  • @OliCharlesworth : Isn't it valid? Commented Feb 28, 2012 at 3:12

1 Answer 1

2

From the C99 Standard Section 6.7.5.2 Array declarators, paragraph 1:

  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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.