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