#include <stdio.h>
#include <stdlib.h>
struct test{
int data;
struct test* next;
};
void add(struct test* L1,struct test* new,int num)
{
int i = 1;
while( num>0 ){
L1->data = i;
new = (struct test*)malloc(sizeof(struct test));
L1->next = new;
new->next = NULL;
L1 = new;
num--;
i++;
}
}
void printfList(struct test* head)
{
struct test* L;
L = head;
while(L->next!=NULL){
printf("%d\n",L->data);
L = L->next;
}
}
int main(int argc,char** argv)
{
if(argc!=2){
perror("prgream is no good\n");
exit(-1);
}
struct test* head;
struct test* new;
//creat head
printf("1\n");
head->data = 1;
printf("2\n");
head->next = NULL;
printf("3\n");
//cha ru other jie dian
add( head,new,atoi( argv[1]) );
//output all L1's data
//printfList(head);
//printf("-------------------------------\n");
//printfList(head);
return 0;
}
我把add( head,new,atoi( argv[1]) );这句话注释掉,就会报段错误;
加上这句话就可以正常运行;
这是为什么呀?
如果不加这句话:printf("1\n");顺利执行,printf("2\n");和printf("3\n");在终端上出不来