求解 为什么函数外与函数内运行结果不一样
求解 为何函数外与函数内运行结果不一样
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "malloc.h"
#define LEN sizeof(struct stu)
int n;
struct stu
{
char name[20];
int num;
long tel;
char email[20];
long qq;
long wx;
struct stu *next;
};
struct stu *creat(void)
{
struct stu *head,*p1,*p2,*p;
char c = 'Y';
FILE *fp;
if((fp = fopen("file_date.txt","w")) == NULL)
{
printf("can not open the file!\n");
exit(0);
}
head = NULL;
n = 0;
p1 = p2 = (struct stu *)malloc(LEN);
printf("请输入新的信息:\n");
scanf("%s%d%ld%s%ld%ld",&p1->name, &p1->num, &p1->tel, &p1->email, &p1->qq,&p1->wx);
printf("请输入Y或N,选择是否继续输入新的信息:\n");
getchar();
scanf("%c",&c);
while(c == 'Y')
{
n++;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct stu *)malloc(LEN);
printf("请输入新的信息:\n");
scanf("%s%d%ld%s%ld%ld",&p1->name, &p1->num, &p1->tel, &p1->email, &p1->qq,&p1->wx);
printf("请输入Y或N,选择是否继续输入新的信息:\n");
getchar();
scanf("%c",&c);
}
if(n == 0)
{
head = p1;
}
n++;
p2->next = p1;
p2 = p1;
p2->next = NULL;
p = head;
while(p != NULL)
{
printf("%s\t%d\t%ld\t%s\t%ld\t%ld\n",p->name, p->num, p->tel, p->email, p->qq, p->wx);
fprintf(fp, "%s\t%d\t%ld\t%s\t%ld\t%ld\n",p->name, p->num, p->tel, p->email, p->qq, p->wx);
p = p->next;
};
free(p1);
fclose(fp);
return(head);
}
void main()
{
struct stu *p;
n=0;
p = creat();
while(p != NULL)
{
printf("%s\t%d\t%ld\t%s\t%ld\t%ld\n",p->name, p->num, p->tel, p->email, p->qq, p->wx);
p = p->next;
}
}
------解决方案--------------------
假设你第一次选Y/N时选了N,那么程序不会执行整个while(c=='Y')循环,在这个while循环后第一句处时:p1==p2, n==0; 然后到p=head语句时 p1==p2 && p1==head, 后一个循环可以正常显示内容,程序继续执行free(p1),然后p1 == p2 && p1 == head && head->已释放内存, 程序退出时返回head.... 也就是说main程序中的p是一个指向已释放内存的指针,你还指望它能正确显示无效内存中的数值吗?
#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "malloc.h"
#define LEN sizeof(struct stu)
int n;
struct stu
{
char name[20];
int num;
long tel;
char email[20];
long qq;
long wx;
struct stu *next;
};
struct stu *creat(void)
{
struct stu *head,*p1,*p2,*p;
char c = 'Y';
FILE *fp;
if((fp = fopen("file_date.txt","w")) == NULL)
{
printf("can not open the file!\n");
exit(0);
}
head = NULL;
n = 0;
p1 = p2 = (struct stu *)malloc(LEN);
printf("请输入新的信息:\n");
scanf("%s%d%ld%s%ld%ld",&p1->name, &p1->num, &p1->tel, &p1->email, &p1->qq,&p1->wx);
printf("请输入Y或N,选择是否继续输入新的信息:\n");
getchar();
scanf("%c",&c);
while(c == 'Y')
{
n++;
if(n == 1)
{
head = p1;
}
else
{
p2->next = p1;
}
p2 = p1;
p1 = (struct stu *)malloc(LEN);
printf("请输入新的信息:\n");
scanf("%s%d%ld%s%ld%ld",&p1->name, &p1->num, &p1->tel, &p1->email, &p1->qq,&p1->wx);
printf("请输入Y或N,选择是否继续输入新的信息:\n");
getchar();
scanf("%c",&c);
}
if(n == 0)
{
head = p1;
}
n++;
p2->next = p1;
p2 = p1;
p2->next = NULL;
p = head;
while(p != NULL)
{
printf("%s\t%d\t%ld\t%s\t%ld\t%ld\n",p->name, p->num, p->tel, p->email, p->qq, p->wx);
fprintf(fp, "%s\t%d\t%ld\t%s\t%ld\t%ld\n",p->name, p->num, p->tel, p->email, p->qq, p->wx);
p = p->next;
};
free(p1);
fclose(fp);
return(head);
}
void main()
{
struct stu *p;
n=0;
p = creat();
while(p != NULL)
{
printf("%s\t%d\t%ld\t%s\t%ld\t%ld\n",p->name, p->num, p->tel, p->email, p->qq, p->wx);
p = p->next;
}
}
------解决方案--------------------
假设你第一次选Y/N时选了N,那么程序不会执行整个while(c=='Y')循环,在这个while循环后第一句处时:p1==p2, n==0; 然后到p=head语句时 p1==p2 && p1==head, 后一个循环可以正常显示内容,程序继续执行free(p1),然后p1 == p2 && p1 == head && head->已释放内存, 程序退出时返回head.... 也就是说main程序中的p是一个指向已释放内存的指针,你还指望它能正确显示无效内存中的数值吗?