1

My teacher said that I can't do that when I'm declaring an array:

int flag_x =0 , flag_y = 0 , x=2, y=2, principal[x][y];

He said I need to user define to use static array , like that:

#define x 2
#define y 2

int flag_x =0 , flag_y = 0 , principal[x][y];

Is that right?

2
  • Yes, your instructor is right unless the compiler is not supporting VLA. Commented May 27, 2014 at 21:06
  • It depends whether this is at file scope, or inside a function Commented May 27, 2014 at 21:51

2 Answers 2

2

He is right. The number of elements in an array declaration with static storage duration (e.g., declared at file scope) has to be an integer constant expression.

If the array has automatic storage duration, the number of element doesn't have to be an integer constant expression:

void foo(void)
{
    int x=2, y=2, principal[x][y];  // valid
}
Sign up to request clarification or add additional context in comments.

Comments

0

It depends.

Variable length arrays have been around since C99. But you can't have a global variable length array (which seems to be the case on your example) - these are only supported in function scope. So, in that sense, your instructor is right, but he could have gone a little bit further and explain that C99 allows you to do that inside a function.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.