警告:格式 %s 需要 char * 类型,但参数 2 具有 int 类型
我已经看过其他相关问题,但没有一个对这个案例有帮助。
我收到问题标题中列出的警告,我的 main
代码如下:
int main( int argc, char *argv[] ) {
char *rows;
int i, n;
printf("\nEnter the amount of rows in the telephone pad: ");
scanf("%d", &n);
rows = (char *) malloc( sizeof( char ) * n );
printf("\nNow enter the configuration for the pad:\n");
for( i = 0; i < n; i++ ) {
scanf("%s", &rows[i]);
printf("\n\t%s\n", rows[i]);
}
return 0;
}
用户要输入一个数字(例如 4),该数字将被扫描到 n
。该空间被分配给电话键盘的行。然后,用户将输入n
行数来配置电话键盘。一个例子是:
123
456
789
.0。
所以我很困惑为什么我的最后一个 printf 语句会出现此错误。
注意:我也尝试了 scanf("%s", rows[i]);
:仍然出现错误。
注2:无论如何,我尝试运行该程序。出现分段错误。
注 3:我的 .c 程序顶部有 #include
和 #include
。
注 4:我已经对程序进行了 gcc 编译,如下所示:gcc -ansi -pedantic -Wall tele.c
。
谢谢你的帮助。
I have already looked at other related questions, and none of them helped this case.
I am getting the warning listed in the title of my question, and my code for main
is as follows:
int main( int argc, char *argv[] ) {
char *rows;
int i, n;
printf("\nEnter the amount of rows in the telephone pad: ");
scanf("%d", &n);
rows = (char *) malloc( sizeof( char ) * n );
printf("\nNow enter the configuration for the pad:\n");
for( i = 0; i < n; i++ ) {
scanf("%s", &rows[i]);
printf("\n\t%s\n", rows[i]);
}
return 0;
}
The user is to enter a number (say, 4), which will be scanned into n
. The space is malloc'ed for the rows of the telephone pad. The user then will enter the n
amount of rows for the configuration of the telephone pad. An example would be:
123
456
789
.0.
So I am confused as to why my last printf
statement is getting this error.
Note: I also tried scanf("%s", rows[i]);
: still got the error.
Note 2: I tried running the program anyways. Got a segmentation fault.
Note 3: I have #include <stdio.h>
and #include <stdlib.h>
at the top of my .c program.
Note 4: I have gcc'ed the program as such: gcc -ansi -pedantic -Wall tele.c
.
Thank you for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
rows[i]
不是char*
——它不是“字符串”。(并且一个字符中不能容纳 3 个字符(加上空终止符)。)
rows[i]
isn't achar*
-- it's not a "string".(And you can't fit 3 characters (plus null terminator) in one character.)
正如其他人指出的那样,您正在为
n
个字符分配空间,但您确实需要为n
行分配空间,每行 4 个字符(用户输入的 3 个字符,以及空终止符)。有几种方法可以做到这一点。您可以首先分配
n
char *
变量来指向行,然后为每行分配 4 个字节:或者,您可以分配
n
4 -前面的字符数组:As others have pointed out, you are allocating space for
n
chars, but you really want space forn
rows with 4 chars each (3 characters entered by the user, and a null terminator).There's a couple of ways to do this. You can first allocate
n
char *
variables to point to the rows, then allocate 4 bytes for each row:Or, you can allocate
n
4-character arrays up front:的 %s
你的 printf 传递的是一个字符,而不是你所说的获取字符串“行”中的第 i 个字符
。更重要的是,你的整个技术将会严重失败......
我认为你想要一个字符串数组......或者你想将你的 scanf 更改为 %c
your printf is passing in a char, not a %s
you are saying get the i'th char in the string 'rows'.
More importantly, your whole technique is going to fail badly....
I think you want an array of strings.... or you want to change your scanf to a %c