柔性数组 data[0]

时间:2023-03-09 00:25:04
柔性数组 data[0]

struct MyData 
{
    int nLen;
    char data[0];
};

在结构中,data是一个数组名;但该数组没有元素;该数组的真实地址紧随结构体MyData之后,而这个地址就是结构体后面数据的地址(如果给这个结构体分配的内容大于这个结构体实际大小,后面多余的部分就是这个data的内容);这种声明方法可以巧妙的实现C语言里的数组扩展。

这表明data没有占用空间。data是一个数组名;该数组没有元素;该数组的真实地址紧随结构体Info之后;这种声明方法可以巧妙的实现C语言里的数组扩展。

实际用时采取这样:
         struct MyData *p = (struct MyData *)malloc(sizeof(struct MyData )+strlen(str))
         这样就可以通过p->data 来操作这个str。

示例:
#include <iostream>

using namespace std;

struct MyData 
{
    int nLen;
    char data[0];
};

int main()
{
    int nLen = 10;
    char str[10] = "123456789";

cout << "Size of MyData: " << sizeof(MyData) << endl;

MyData *myData = (MyData*)malloc(sizeof(MyData) + 10);
    memcpy(myData->data,  str, 10);

cout << "myData's Data is: " << myData->data << endl;

free(myData);

return 0;
}
         输出:
Size of MyData: 4
myData's Data is: 123456789

C99标准为了让这种用法合法化,专门定义了flexible array member,直接就是
struct Name { int size, char foo[] }; 不再写 char foo[0] 了。

它只能放在结构体末尾, 
是申明一个长度为0的数组(eg. char data[0] ;或 int data[0];都行) 
可以使得这个结构体是可变长的。

引申:

 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <string.h>
 #include <iostream>
 using namespace std;
 typedef struct test0{
     ];
     int b;
     char c;
 }sttest0;
 typedef struct test{
     ];
     ];
 }sttest;
 typedef struct test1{
     ];
     ];
 }sttest1;
 typedef struct test2{
     struct test tt;
     struct test1 tt1;
 }sttest2;

 int main()
 {
     ] = ";
     int len1 = sizeof(str);
     cout << len1 << endl;
     ] ="abcdefghij";
     int len2=sizeof(str2);
     cout << len2 << endl;
     printf("%lu %lu %lu\n", sizeof(sttest0), sizeof(sttest), sizeof(sttest1));
     cout << "Size of sttest2: " << sizeof(sttest2) << endl;
     sttest2 *tt2 = (sttest2*));

     memcpy(tt2->tt.a, str, len1);
     memcpy(tt2->tt1.a , str2, len2);

     //需要注意的是两者位置互换后的情况
 //    memcpy(tt2->tt1.a , str2, len2);
 //    memcpy(tt2->tt.a, str, len1);

     printf("#################value\n");
     printf("tt:%s\n%s\n", tt2->tt.a, tt2->tt.b);
     printf("tt1:%s\n%s\n", tt2->tt1.a, tt2->tt1.b);

     printf("#################address\n");
     printf("addr(tt2):%#x\n%#x\n", &tt2->tt, &tt2->tt1);
     printf("addr(tt):%#x\n%#x\n", &tt2->tt.a, &tt2->tt.b);
     printf("addr(tt1):%#x\n%#x\n", &tt2->tt1.a, &tt2->tt1.b);
     free(tt2);
     ;
 }

柔性数组 data[0]

相关文章