汉扬编程 C语言入门 C语言变长数组data「0」总结

C语言变长数组data「0」总结

变长数组有的编译器不支持数组长度为0的,比如keil;而QT和linux是支持的。

这种方式主要运用在数据长度可变的通信领域很有用,通过动态内存申请,来增强内存的实际使用率。

以下是个运用实例,请参考。

#if 1

typedef struct

{

int data_len;

char data[0];

}buff_st_1;

typedef struct

{

int data_len;

char *data;

}buff_st_2;

typedef struct

{

int data_len;

char data[];

}buff_st_3;

typedef struct

{

uint32_t id;

uint32_t age;

}student_st;

void print_stu(const student_st *stu)

{

printf("id:%u,age:%u\\n", stu->id, stu->age);

}

void testStu(void)

{

student_st *stu = (student_st *)malloc(sizeof(student_st));

stu->id = 100;

stu->age = 23;

student_st *tmp = NULL;

buff_st_1 *buff1 = (buff_st_1 *)malloc(sizeof(buff_st_1) + sizeof(student_st));

buff1->data_len = sizeof(student_st);

memcpy(buff1->data, stu, buff1->data_len);

printf("buff1 address:%p,buff1->data_len address:%p,buff1->data address:%p\\n",

buff1, &(buff1->data_len), buff1->data);

tmp = (student_st*)buff1->data;

print_stu(tmp);

buff_st_2 *buff2 = (buff_st_2 *)malloc(sizeof(buff_st_2));

buff2->data_len = sizeof(student_st);

buff2->data = (char *)malloc(buff2->data_len);

memcpy(buff2->data, stu, buff2->data_len);

printf("buff2 address:%p,buff2->data_len address:%p,buff2->data address:%p\\n",

buff2, &(buff2->data_len), buff2->data);

tmp = (student_st *)buff2->data;

print_stu(tmp);

buff_st_3 *buff3 = (buff_st_3 *)malloc(sizeof(buff_st_3) + sizeof(student_st));

buff3->data_len = sizeof(student_st);

memcpy(buff3->data, stu, buff3->data_len);

printf("buff3 address:%p,buff3->data_len address:%p,buff3->data address:%p\\n",

buff3, &(buff3->data_len), buff3->data);

tmp = (student_st*)buff1->data;

print_stu(tmp);

free(buff1);

free(buff2->data);

free(buff2);

free(buff3);

free(stu);

}

#endif

本文来自网络,不代表汉扬编程立场,转载请注明出处:http://www.hyzlch.com/cjia/6444.html

求NOIP2008普及组第四题C语言代码。

C语言编程之函数调用规则

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

返回顶部