struct {
char a[10];
char b[5];
char c[10];
} info;
如何将所有struct
数据成员连接成一个单独的数组?
答案 0 :(得分:5)
使用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));
答案 1 :(得分:0)
你可以使用sprintf。此函数将字符串'打印'到另一个字符串: