将结构成员复制到数组

struct { char a[10]; char b[5]; char c[10]; } info; 

如何将所有struct数据成员连接成一个单独的数组?

使用memcpy()

 // Assign a buffer big enough to hold everything char *buf = malloc(sizeof(info.a) + sizeof(info.b) + sizeof(info.c)); // Get a pointer to the beginning of the buffer char *p = buf; // Copy sizeof(info.a) bytes of stuff from info.a to p memcpy(p, info.a, sizeof(info.a)); // Advance p to point immediately after the copy of info.a p += sizeof(info.a); // And so on... memcpy(p, info.b, sizeof(info.b)); p += sizeof(info.b); memcpy(p, info.c, sizeof(info.c)); 

你可以使用sprintf。 这个function’打印’一个字符串到anoter:

 int struct_size = sizeof(info); char *result = (char*)malloc(sizeof(char)*struct_size); sprintf(result, "%s%s%s", info.a, info.b, info.c);