简单的赋值操作,有点不明白,在线等。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
struct helloworld_t
{
int num;
char helloworld[0];
};
int main()
{
struct helloworld_t *p;
unsigned int size = sizeof(struct helloworld_t) + strlen("Hello World!\n") + 1;
p = (struct helloworld_t *) malloc(size);
assert(p!=NULL);
memcpy(p, "\x03\x00\x00\x00Hello World!\n", size);
while (p->num--)
{
printf(p->helloworld);
}
//printf("%d \n", sizeof(helloworld_t));
free((void *)p);
return 0;
}
输出3个Hello World!
如果把 memcpy(p, "\x03\x00\x00\x00Hello World!\n", size);改成
memcpy(p, "\x02\x00\x00\x00Hello World!\n", size);
则输出两个Hello World!
换成1,则输出一个Hello World!
不太明白 memcpy(p, "\x03\x00\x00\x00Hello World!\n", size); 这句话的意思,是给num赋值操作码?拼什么也能这样赋值?不懂,等待高手的回答!