0

What actually happens when you declare an array with empty bounds of type int for example? How does your computer manage the memory allocated to that specific empty array?

int A[];
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Kamoukou
  • 123
  • 1
  • 2
  • 6
  • Possible duplicate of [What happens if I define a 0-size array in C/C++?](http://stackoverflow.com/questions/9722632/what-happens-if-i-define-a-0-size-array-in-c-c) - edit: maybe not so memory related, though. – Marvin Nov 11 '16 at 23:36
  • 3
    `int A[];` is not a valid declaration in C. The compiler should emit a diagnostic message. – n. m. could be an AI Nov 11 '16 at 23:54
  • @n.m. It's valid as a struct member for C99 and later – kdopen Nov 12 '16 at 00:22
  • 1
    The only time you can write that is if you have an old-style, pre-standard, non-prototype function definition such as `int somefunc(A) int A[]; { … }`. Doing that in new code would be extremely bad practice. There, it means an array passed to the function, equivalent to `int somefunc(int A[]) { … }` in prototype style. If prefixed with the keyword `extern`, it means that the array exists and is defined somewhere else (but that's different from what you wrote). Otherwise, it is simply invalid C. You cannot specify an explicit array size of 0 either in standard C (but GCC allows you to do it). – Jonathan Leffler Nov 12 '16 at 06:40

1 Answers1

0

Inside a function body, int A[]; is a constraint violation.

Outside of a function, a variable definition with no initializer is called a tentative definition. This one defines that A is an array of int of unknown bound. The type of A is an incomplete type because the bound is not known yet. It may be completed later:

int A[];

void f()
{
     A[0] = 1;
}

int A[5];

If the latter definition is omitted, the program behaves as if the source file had int A[1]; at the end.

M.M
  • 138,810
  • 21
  • 208
  • 365