我用指针和函数制作了这个程序,它应该是一个链表。我不断收到“访问冲突读取位置 0xcdcdcded”。在下面的最后一部分。我认为可能是我下一步没有初始化,但我是指针新手,不知道该怎么做。任何帮助是极大的赞赏。
typedef struct temp
{
char name[20];
char telephone[10];
temp *next;
} node;
node* creation1 ()
{
node *NEW = NULL;
NEW = (node*)malloc(sizeof(node));
return NEW;
}
node* creation2 ()
{
node *start= NULL;
node *NEW = creation1();
start= NEW;
return start;
}
node* creation3 ()
{
node *NEW = creation1();
node *current = NULL;
current=NEW;
return current;
}
void consult ()
{
node *NEW= creation1();
node *start= creation2();
node *current = creation3();
int exit;
printf("How many contacts do you wish to add? ");
scanf("%i",&exit);
for(int i=1; i<=exit; i++)
{
NEW = (node*)malloc(sizeof(node));
current->next=NEW;
current = NEW;
fflush(stdin);
puts("NAME: ");
gets(NEW->name);
puts("TELEPHONE: ");
gets(NEW->telephone);
NEW->next=NULL;
}
current=start->next;
int i = 0;
do
{
i++;
current = current->next; //this is where it stops and gives me the access reading violation
}while (current != NULL);
}
int main(int argc, char** argv)
{
consult();
}