为什么一个字符数组strcpy的需要和char星不 - 在C使用结构 [英] Why does a char array need strcpy and char star doesn't - using structs in C
本文介绍了为什么一个字符数组strcpy的需要和char星不 - 在C使用结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对本code一个误区 -
I have a misunderstanding regarding this code -
typedef struct _EXP{
int x;
char* name;
char lastName[40];
}XMP
...main...
XMP a;
a.name = "eaaa";
a.lastName = strcpy(a.lastName, "bbb");
为什么我不能用:<?code> a.lastName =BBBB; ,这一切
推荐答案
好考虑的类型在这里。该阵列具有的字符串的内容,而的char *
仅仅指向的数据。因此,该阵列需要的strcpy
和朋友。
Well consider the types here. The array has the contents of the string, while the char*
merely points to the data. Consequently the array requires strcpy
and friends.
此外,如果你的的char *
在堆上分配的内存或堆栈,然后想一些内容分配给,你也可以选择使用的strcpy
,因为一个单纯的分配将创建一个悬空指针(即内存泄漏)。
Besides, if you allocated memory for the char*
on the heap or stack and then wanted to assign some content to that, you'd also have to use strcpy
because a mere assignment would create a dangling pointer (i.e. a memory leak).
这篇关于为什么一个字符数组strcpy的需要和char星不 - 在C使用结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文