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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

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
}
ouah
  • 142,963
  • 15
  • 272
  • 331
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.

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70