使用malloc和struct的分段错误(核心已转储)

我不明白为什么这个简单的代码没有在linux上导致分段错误的情况下无法运行:


#include <stdlib.h>


struct entry

{

   int value;

};


void initEntry(struct entry *entry)

{

  entry = malloc(sizeof(struct entry));    

  entry->value = 0;

}


int main()

{

  struct entry *list;


  initEntry(list);    

  list->value = 5;

}

删除最后一条指令(list->value = 5;)后,我可以运行该程序


我编译:


gcc main.c -o main


largeQ
浏览 292回答 2
2回答

慕丝7291255

代码运行,但是我在编译过程中main.c:31:13: warning: passing argument 1 of ‘initEntry’ from incompatible pointer type [-Wincompatible-pointer-types] &nbsp; &nbsp;initEntry(&list); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;^ main.c:24:6: note: expected ‘struct entry *’ but argument is of type ‘struct entry **’ &nbsp;void initEntry(struct entry *entry) {
打开App,查看更多内容
随时随地看视频慕课网APP