2

Possible Duplicate:
zero length arrays vs. pointers

Some new compilers throw up compilation error for below case

struct test {
int length;
char data[0];
};

int main(void)
{
char string[20] = {0};
struct test *t;

//Some code

memcpy(string, t->data, 19); //Compilation error 
}

However this gets resolved if I do like this.

memcpy(string, &(t->data[0]), 19);

Any reason why some new compilers are enforcing this restriction?

Edited to correct mistakes

Community
  • 1
  • 1
nkumar
  • 51
  • 2

2 Answers2

6

What's wrong with this:

struct test t;

memcpy(string, test->data, 19);

? Hint, test is a type.

EDIT: as to the real answer, see this question: zero length arrays vs. pointers (or similar questions on SO)

Community
  • 1
  • 1
Nim
  • 33,299
  • 2
  • 62
  • 101
0

Array can't have 0 size .

Here is the Standard:

ISO 9899:2011 6.7.6.2:

   If the expression is a constant expression, it shall have a value
   greater than zero

And second

use this:

memcpy(string,t->data,19); instead of what you have used.
Omkant
  • 9,018
  • 8
  • 39
  • 59
  • Yeah but this is in ISO c, the concept of zero lengh arrays to indicate a member which can latter be malloced is common. ISO C allows using array[]; for that (note no size given) where as gcc allows array[0]; – fkl Nov 21 '12 at 10:35