Struct dirent {
.......
.......
Char A [1];}
Here char a [1]; the length is 1. It is generally considered that the character array can only store '/0', But here a [1] declares
Placed at the end of the struct, It is a variable-length data structure, which facilitates access to the address behind the struct, such:
[Copy to clipboard] [-] Code: struct dirent
{
Int Len;
Char A [0];
}; Struct dirent * Fun (char * STR, int Len)
{
Struct dirent * n = (struct dirent *) malloc (LEN + 1 + sizeof (struct dirent ));
If (! N)
Return NULL;
N-> Len = Len;
Memcpy (n-> A, STR, Len );
Return N;
} The size is extended when malloc is used, which is exactly at the end of struct (char a [0] or char a [1] must be the final member of the struct ),
In the memory, the extended address is adjacent to the last member char P [0], that is, access can be achieved through P,
The advantage of char P [0] Over char * P is:
(1) The former malloc does not need to be assigned to P, because the former is array.
(2) The former implements a dynamic array function. If it is not needed, it can occupy no memory at all, and the latter will occupy 4 bytes.
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.