我正在与C.你能告诉我,如果这是正确的方式来分配内存的struct
其中包含一个字符串?字符串动态分配在C
struct _TipoLista {
char info[10];
struct _TipoLista *next;
};
typedef struct _TipoLista *TipoLista;
...
TipoLista el;
el = malloc(sizeof(TipoLista));
如果试图以这种方式创建一个列表,当我尝试插入第二个元素的时候总是会出错。但是,如果我将“信息”从char[10]
更改为int
,我的代码始终有效。
-1,这倒像是Java的。当你有一个指向它的指针时,你不能使用'.'来访问结构的字段,你必须使用' - >'。另外,在解引用它们之前,'NULL'检查指针是一种很好的做法。 – unwind
@unwind:你说得对,“ - >”...对不起 – Exceptyon