0

In the following two struct definition, what is the difference among them?

struct A {
    int var[];
};

struct B {
    int var[0];
};

struct C {
    int *var;
};

I think in case of using those structures, they are the same. Is there a special meaning behind them?

jaeyong
  • 8,951
  • 14
  • 50
  • 63

4 Answers4

4

The difference between A and B is that the second one is illegal. Array declarations that specify 0 as the size are always unconditionally illegal in standard C. If your compiler accepts this declaration, it is a non-standard extension of your compiler.

Meanwhile, the array declaration in A is a C99 feature that declares a flexible array member at the end of the struct.

The declaration inside C is a pointer declaration. A and B declare array members, C declares a pointer member. So, that's your difference.

P.S. If your compiler accepts the definition of B, then it is indeed likely to be the same as the definition of A.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • Could you also describe the difference betweew struct A and C? – jaeyong Nov 08 '13 at 07:07
  • @jaeyong: I don't see what to describe besides the obvious: the first two contain arrays, while `C` contains a pointer. That's all. I hope you understand the difference between an array and a pointer. These are just two different things. – AnT stands with Russia Nov 08 '13 at 07:10
  • @jaeyong: A flexible array member is a place where an array will be, if space is allocated for it along with the rest of the structure. A pointer is an object where an address can be stored. Space would have to be allocated for an array, them the address of that array would have to be stored in the pointer. The array would not be at the place where the pointer is. – Eric Postpischil Nov 08 '13 at 07:29
2
 int var[] -> variable length array 

 int var[0] -> does not have any element in the array // Illegal

 int *var -> it is an integer pointer that can store the address of integer
              or base address of array of integers

 var[] VS *var

  var[] -> array of integers, 

           var[0],var[1] -> this is how you access elements of array

   *var -> needs initiailization of an address of an integer before it is used

        1) int *var = &a; // if you have one integer             
        2) int *var = &a[0] // if you have array of integers save base address
            var[1] or *(++var) // both are equivalent

The advantage of integer pointer over integer array is to return an int pointer
      in function calls because retuning array of integers is not possible

Arrays must have atleast 1 element. Your int var[0] is not a valid way for defining array length. int var[1] is valid

niko
  • 9,285
  • 27
  • 84
  • 131
0

As pointed by others, second declaration is not legal. This trick is generally used for variable sized structure.

Same can be achieved in legal way by declaring array of size 1.

This question can help you understand about it.

To know more, google variable sized structure or variable length structure.

Community
  • 1
  • 1
doptimusprime
  • 9,115
  • 6
  • 52
  • 90
0

While zero length arrays are illegal in C, they are supported in GCC. Some relevant info to your question is available here http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html

user12321
  • 78
  • 8