下面是我学习linux环境中c语言的程序,但是运行后提示说‘段错误’,请大家看看,谢谢了
dsdc 2006-12-21 11:15:23 下面是我学习linux环境中c语言的程序,但是运行后提示说‘段错误’,请大家看看,谢谢了
/*
programe: oplist.c
comment : operate stack,include:
1.create new node;
2.push a node;
3.pop a node;
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct finfo
{
char f_name[1024];
struct finfo *next;
};
struct finfo *createnode(char *buf)
{
struct finfo *info;
int i;
info = (struct finfo*)malloc(sizeof(info));
if (info != NULL)
{
info->next = NULL;
strcpy(info->f_name,buf);
//printf("%s,",info->f_name);
//printf("%d,%d\n",strlen(buf),strlen(info->f_name));
return info;
}
else
return NULL;
}
void push(struct finfo **head,struct finfo **node)
{
if (head == NULL)
{
*head = *node;
(*head)->next = NULL;
}
else
{
(*node)->next = *head;
*head = *node;
}
}
struct finfo *pop(struct finfo **head)
{
struct finfo *tmp;
if (*head == NULL)
return NULL;
else
{
tmp = *head;
*head = tmp->next;
//printf("%s\n",tmp->f_name);
return tmp;
}
}
main()
{
char *buf;
struct finfo *head;
struct finfo *node;
head = NULL;
node = NULL;
int j;
buf = (char*)malloc(50*sizeof(char));
printf("ready to copy.");
for (j=1;j<1025;j++)
{
strcpy(buf,"list21234567890.c");
node = createnode(buf);
push(&head,&node);
}
free(buf);
printf("display info:\n");
node = pop(&head);
while (node != NULL)
{
printf("%s\n",node->f_name);
free(node);
node = pop(&head);
}
printf("pop over.\n");
}