大家帮我看看,我的程序有点怪异,我用链表实现calloc函数,
calloc一次分配N字节的内存,使用一个结构指针p指向它分配的首地址,用一个结构指针数组str[N]同时指向它的首地址,使用sizeof(struct list p)求出寸入每个链表结构里数据的长度,依次用首地址加这个长度,到了最后一次的时候,我多使用了一个内存结构,因为我是按地址加加的,所以理论上多出的内存存在,我再释放它,
现在问题是,当建立链表模块返回头节点的时候,单步调试时,头节点内数据都满足要求,可是在main()里返回来的就成了NULL了,我把返回的head改名为phead,这个问题就不存在了,请高手指教!!我知道可以用数组实现这个函数,那样就没有什么意义了,我只是想理解链表。
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
#define N 5struct list
{
int data;
struct list *next;
};/*链表建立模块*/
struct list *created()
{
struct list *head;
int i;
struct list *p;
struct list *str[N];
p=(struct list *)calloc(N,sizeof(struct list));
head=p;
str[0]=p;
for(i=0;i<N;i++)
{
printf("please enter a data:\n");
scanf("%d",&str[i]->data);
str[i+1]=str[i]+(sizeof(p));
str[i]->next=str[i+1];
}
str[i-1]->next=NULL;
return head;
}/*打印模块*/
struct list *printed(struct list *head)
{
struct list *p;
p=head;
printf("The data you put are:");
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
printf("\b\b ");
return head;
}/*硬盘数据写入模块*/
struct list *writed(struct list *head)
{
struct list *p;
p=head;
FILE *fp;
fp=fopen("c:\\1.txt","a+");
if(fp==NULL)
{
printf("Error open file!\n");
exit(0);
}
while(p!=NULL)
{
fprintf(fp,"%d\n",p->data);
p=p->next;
}
printf("The file had been well done!\n");
return head;
fclose(fp);
}/*内存空间释放模块*/
void freed(struct list *head)
{
struct list *p;
p=head;
free(p);
}/*主函数体*/
main()
{
struct list *phead;
phead=NULL;
phead=created();
phead=printed(phead);
phead=writed(phead);
freed(phead);
return 1;
}
calloc一次分配N字节的内存,使用一个结构指针p指向它分配的首地址,用一个结构指针数组str[N]同时指向它的首地址,使用sizeof(struct list p)求出寸入每个链表结构里数据的长度,依次用首地址加这个长度,到了最后一次的时候,我多使用了一个内存结构,因为我是按地址加加的,所以理论上多出的内存存在,我再释放它,
现在问题是,当建立链表模块返回头节点的时候,单步调试时,头节点内数据都满足要求,可是在main()里返回来的就成了NULL了,我把返回的head改名为phead,这个问题就不存在了,请高手指教!!我知道可以用数组实现这个函数,那样就没有什么意义了,我只是想理解链表。
#include <stdio.h>
#include <stdlib.h>
#define NULL 0
#define N 5struct list
{
int data;
struct list *next;
};/*链表建立模块*/
struct list *created()
{
struct list *head;
int i;
struct list *p;
struct list *str[N];
p=(struct list *)calloc(N,sizeof(struct list));
head=p;
str[0]=p;
for(i=0;i<N;i++)
{
printf("please enter a data:\n");
scanf("%d",&str[i]->data);
str[i+1]=str[i]+(sizeof(p));
str[i]->next=str[i+1];
}
str[i-1]->next=NULL;
return head;
}/*打印模块*/
struct list *printed(struct list *head)
{
struct list *p;
p=head;
printf("The data you put are:");
while(p!=NULL)
{
printf("%d->",p->data);
p=p->next;
}
printf("\b\b ");
return head;
}/*硬盘数据写入模块*/
struct list *writed(struct list *head)
{
struct list *p;
p=head;
FILE *fp;
fp=fopen("c:\\1.txt","a+");
if(fp==NULL)
{
printf("Error open file!\n");
exit(0);
}
while(p!=NULL)
{
fprintf(fp,"%d\n",p->data);
p=p->next;
}
printf("The file had been well done!\n");
return head;
fclose(fp);
}/*内存空间释放模块*/
void freed(struct list *head)
{
struct list *p;
p=head;
free(p);
}/*主函数体*/
main()
{
struct list *phead;
phead=NULL;
phead=created();
phead=printed(phead);
phead=writed(phead);
freed(phead);
return 1;
}
{
struct list *head;
int i;
struct list *p;
struct list *str[N];
// calloc是分配动态数组的做法,不是分配链表空间的做法
p=(struct list *)calloc(N,sizeof(struct list)); head=p;
str[0]=p;
for(i=0;i<N;i++)
{
printf("please enter a data:\n");
scanf("%d",&str[i]->data);
// str已经是(struct list *)类型了,所以移到下一个元素只需+1,
// 不能+(sizeof(p)),极有可能是这引起的错误。
str[i+1]=str[i]+(sizeof(p)); str[i]->next=str[i+1];
}
str[i-1]->next=NULL;
return head;
}
----------------------------
不明白为什么要用这个数组.完全可以从头指针得到长度吗