gpt4 book ai didi

c - C 中的链表 - 插入名称会使程序崩溃

转载 作者:行者123 更新时间:2023-11-30 15:19:34 25 4
gpt4 key购买 nike

该代码适用于菜单中的所有其他选项,但插入名称和显示名称是一个问题,因为如果我不执行 strcpy,它会显示随机字符...我只是对逻辑在哪里感到困惑这个链表出了问题。我将感谢您的帮助!

#include<stdio.h>
#include<stdlib.h>



// Main function that displays menu
int main()
{
int input;
char user;
while(1)
{
printf("\nMenu of Operations\n");
printf("1. Insert\n");
printf("2. Display\n");
printf("3. Count\n");
printf("4. Delete\n");
printf("5. Exit\n");
printf("/////////////////////////////////////\n\n");
printf("Please enter the operation you would like: ");

if(scanf("%d", &input) >= 0)
{
switch(input)
{

case 1:
printf("Enter the name to insert: ");
scanf("%s", &input);
insert(user);
break;
case 2:
if(head == NULL)
{
printf("List is Empty\n");
}
else
{
printf("Element(s) in the list are : ");
}
display(n);
break;

default:
printf("Invalid option\n");
break;
}
}

}

return 0;
}

最佳答案

您在 case 1 中有一个拼写错误,你经过的地方&input而不是&user 。但这对你没有多大帮助。

user是单char ,但你会把它给 scanf()就像它是一个字符数组一样。将其更改为 char user[N] ,其中N大于您期望的最大长度(可能是 50,如 person )。

然后,您将需要更改大多数其他函数以获取 const char*而不是char 。这也将修复对 strcpy() 的调用。 ,它不会像现在这样编译,因为他们期望第二个参数是 const char* .

关于c - C 中的链表 - 插入名称会使程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30581327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com