Possible Duplicate:
What is the difference between char s[] and char *s in C?
Difference between char *str = “…” and char str[N] = “…”?字符串在结构用C
我有一个结构定义为:
typedef struct
{
bool configured;
bool active;
uint32 lastComms;
uint32 rxChRvdTime;
char *name;
}vehicle;
,我初始化如下:
static vehicle *myVehicle;
当我要初始化的名字,我用途:
myVehicle->name = "helloworld";
这工作正常。但是当我不想将它设置为字符串字面以外的东西时,我似乎遇到了问题。
char *tmpName = "foobar";
strcpy(myVehicle->name, tmpName);
那么为什么不strcpy工作?我是否需要事先在结构中预先分配结构中的字符串大小?我不应该使用'姓名'字段的指针,因为只能有一辆车?
__“我是否需要事先在结构中预先分配结构中的字符串大小?”__ – oldrinb
ok,noob问题:为什么不简单地执行'myVehicle-> name = tmpName;' – Prasanth
我不同意。这个问题是关于在结构体内为字符串分配内存的。我读了这个问题,但并没有解决我的问题。 – Jonathan