#include <stdio.h>
#include <stdlib.h>
typedef struct s{
int n;
}F;
F* make();
void create(F *s);
void add(F *f);
void show(F *f);
int main()
{
F *f=NULL;
//1.) the following doesn't work
create(f);
show(f);
//2.) The Following is work
f=make();
show(f);
printf("Hello world!\n");
return 0;
}
void add(F *f){
(f->n)++;
}
void show(F *f){
printf("\n======\n %d \n======\n",f->n);
}
F* make(){
F * temp=(F*)malloc(sizeof(F));
temp->n=19;
return temp;
}
void create(F *s){
F * temp=(F*)malloc(sizeof(F));
temp=make();
s=temp;
show(s);
}
请解释为什么代码片段(1)说碎片错误(我知道这是关于访问无效的内存空间,但是通过查看我自己的代码,我不知道错误在哪里。),但是(2)没关系,它工作正常。先感谢您 。