【发布时间】:2015-01-23 18:28:11
【问题描述】:
我不明白为什么当我运行此代码时,printf 语句不起作用。
代码如下:
typedef struct list {
int n;
struct list *next;
}List;
List **head;
List *tmp=malloc(sizeof(List));
tmp->n=34;
tmp->next=NULL;
List *tmp2=malloc(sizeof(List));
tmp2->n=45;
tmp2->next=NULL;
List *tmp3=malloc(sizeof(List));
tmp3->n=26;
tmp3->next=NULL;
head=malloc(sizeof(head));
head[0]=tmp;
head[1]=tmp2;
head=realloc(head,sizeof(head));
head[2]=tmp3;
printf("n of tmp:%d \n",head[0][0].n);
printf("n of tmp2:%d \n",head[1][0].n);
printf("n of tmp3:%d \n",head[2][0].n);
我认为原因可能是realloc,但为什么呢?我正在正确使用它,不是吗?我已经按照这个教程http://www.tutorialspoint.com/c_standard_library/c_function_realloc.htm
【问题讨论】: