An array of length 0 is used only in the last member of the structure to make an address tag that is used later.

Source: Internet
Author: User
Tags array definition
int array[0] in standard C or C + + because an array of 0 lengths is not supported; Such a definition is illegal. However, some compiler extensions support a 0-length array.

In C, the primary purpose of a 0-length array is to be used as the last member of the structure body, and then to use it to access a segment of memory (usually dynamically allocated memory) after this structure object. Because of its nonstandard nature, try to avoid using 0-length arrays in your program. As a replacement, you can replace the 0-length array definition with an incomplete array in the C99 standard. Such as:
Code:struct X {
* Members * *
int array[]; /* Do not write int array[0]; Since it is not standard. */
};
>> but my code below is capable of compiling without warning and error, and can run.
>> int array[0]
>> *array = 1;

There are a number of errors that may occur in your program. One is syntax errors, which can easily be checked at compile time. In addition, there are a class of errors in the compile phase can not or difficult to check out, such errors are called "undefined", the result is uncertain.

In the code above, because there is an error in the array access, its behavior (the result of execution) is undefined. Applications such as:
Code:struct A {
int A, B;
Char data[0];
/* Do not write fields below * *
};

struct A *p;

int n = m, I;
p = malloc (sizeof (struct A) + N);
for (i = 0; i < n; ++i)
P->data[i] = 1;
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.