c - c中的警告称为“'s1'和's2'在此函数中未初始化”
问题描述
无法解决 c 中称为 s1 的警告 s2 is used uninitialized this function
int main()
{
char *s1, *s2;
printf("Please enter string s1 and then s2:\n");
scanf("%s %s", s1, s2);
printf("%s %s", *s1, *s2);
return 0;
}
解决方案
您必须为s1
和分配s2
:
// do not forget to include the library <stdlib.h> for malloc function
s1 = malloc(20); // string length of s1 ups to 19;
if(!s1) {return -1;}
s2 = malloc(20) // // string length of s2 ups to 19 also;
if(!s2) {return -1;}
inscanf
函数,应更改为(scanf 的缺点):
scanf("%19s %19s", s1, s2); // or using fgets
或者您可以使用字符数组而不是使用指针:
char s1[20], s2[20];
// Or you can define a maximum length MAX_LEN, then using:
// char s1[MAX_LEN], s2[MAX_LEN];
推荐阅读
- wpf - 如何使 DataGridCell 具有与其第一个儿子相同的边框
- python - Django update_or_create 不更新而是插入
- android - 未创建 Kotlin Room 数据库
- android - 错误类型 3 - 错误:活动类 MainActivity 不存在
- sql-server - 为满足特定条件的行组合第一行和最后一行的值
- powershell - PowerShell Out-GridView
- symfony - 无法使用 Sonata 管理表单添加
- ubuntu - 在我的电脑上安装 lokinet mixnet 后如何获取我的 lokinet 地址?
- sql - 从更新日期为 MAX 的 50 列以上的表中选择所有行
- node.js - 如何使用 ajax 使用 AWS lambda 和 nodejs 发布到 kintone 端点