|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我想新建立一个链表并在里面添加数字输出,但是总是崩溃,求高手给指点以下那里错了麻烦。
- #include<stdio.h>
- #include<stdlib.h>
- struct Day
- {
- int number;
- struct Day *next;
- };
- void addnumber(struct Day **my);
- void my_scanf(struct Day *may);
- void my_print(struct Day *my);
- int main(void)
- {
- int i;
- struct Day *my; //创建一个头节点
- /*my->next = NULL; //清空头节点中的信息域*/
- for(i = 0; i < 2; i++)
- {
- addnumber(&my);
- }
- my_print(my);
-
- return 0;
- }
- void addnumber(struct Day **my)
- {
- struct Day *may; //创建一个首节点
- struct Day *temp; //创建一个临时节点
- may = (struct Day *)malloc(sizeof(struct Day));
- if(may == NULL)
- {
- printf("内存分配失败!\n");
- exit(1);
- }
- my_scanf(may);
- if(*my != NULL)
- {
- temp = *my;
- *my = may;
- may->next = temp;
- }
- else
- {
- *my = may;
- may->next = NULL;
- }
-
- }
- void my_scanf(struct Day *may)
- {
- printf("请输入一个数字");
- scanf("%d", may->number);
- }
- void my_print(struct Day *my)
- {
- while (my != NULL)
- {
- printf("%d", my->number);
- my = my->next;
- }
- }
复制代码
- 1>------ 已启动全部重新生成: 项目: tmp, 配置: Debug Win32 ------
- 1>main.c
- 1>c:\visualstudioprojects\tmp\tmp\main.c(54): warning C4477: “scanf”: 格式字符串“%d”需要类型“int *”的参数,但可变参数 1 拥有了类型“int”
- 1>c:\visualstudioprojects\tmp\tmp\main.c(54): warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
- 1>c:\program files (x86)\windows kits\10\include\10.0.15063.0\ucrt\stdio.h(1272): note: 参见“scanf”的声明
- 1>c:\visualstudioprojects\tmp\tmp\main.c(66): warning C4477: “printf”: 格式字符串“%d”需要类型“int”的参数,但可变参数 1 拥有了类型“Kong *”
- 1>tmp.vcxproj -> C:\VisualStudioProjects\tmp\Debug\tmp.exe
- 1>已完成生成项目“tmp.vcxproj”的操作。
- ========== 全部重新生成: 成功 1 个,失败 0 个,跳过 0 个 ==========
复制代码
c:\visualstudioprojects\tmp\tmp\main.c(54): warning C4477: “scanf”: 格式字符串“%d”需要类型“int *”的参数,但可变参数 1 拥有了类型“int”
我相信你的编译器也一定有这个警告
54行 scanf 有一个 %d,这个 %d 需要一个 int 指针的参数,但现在这个参数不是int指针,而是int类型
- #include<stdio.h>
- struct Test
- {
- int num;
- char str[10];
- };
- int main(void)
- {
- struct Test test = {0};
- struct Test *p = &test;
- scanf("%d", &test.num);
- scanf("%d", &p->num);
- return 0;
- }
复制代码
多留意警告,编译器不会平白无故报警告
|
|