[英]warning: format %s expects type char * but argument 2 has type int
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: 我在问题标题中列出了警告,而我的
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;
}
The user is to enter a number (say, 4), which will be scanned into n
. 用户将输入一个数字(例如4),该数字将被扫描为
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. 然后,用户将输入
n
行以配置电话键盘。 An example would be: 一个例子是:
123 123
456 456
789 789
.0. 0.0。
So I am confused as to why my last printf
statement is getting this error. 因此,我对为什么我的上一个
printf
语句收到此错误感到困惑。
Note: I also tried scanf("%s", rows[i]);
注意:我也尝试过
scanf("%s", rows[i]);
: still got the error. :仍然出现错误。
Note 2: I tried running the program anyways. 注意2:我还是尝试运行该程序。 Got a segmentation fault.
出现分段错误。
Note 3: I have #include <stdio.h>
and #include <stdlib.h>
at the top of my .c program. 注意3:我的.c程序顶部有
#include <stdio.h>
和#include <stdlib.h>
。
Note 4: I have gcc'ed the program as such: gcc -ansi -pedantic -Wall tele.c
. 注意4:我已经将该程序
gcc -ansi -pedantic -Wall tele.c
: gcc -ansi -pedantic -Wall tele.c
Thank you for the help. 感谢您的帮助。
rows[i]
isn't a char*
-- it's not a "string". rows[i]
不是char*
-不是“字符串”。
(And you can't fit 3 characters (plus null terminator) in one character.) (而且一个字符不能容纳3个字符(加上空终止符)。)
As others have pointed out, you are allocating space for n
chars, but you really want space for n
rows with 4 chars each (3 characters entered by the user, and a null terminator). 正如其他人指出的那样,您正在为
n
字符分配空间,但是您确实想要n
行的空间,每行4个字符(用户输入3个字符,并且使用空终止符)。
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: 您可以先分配
n
char *
变量以指向行,然后为每行分配4个字节:
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 = malloc( n * sizeof rows[0] );
printf("\nNow enter the configuration for the pad:\n");
for( i = 0; i < n; i++ ) {
rows[i] = malloc(4);
scanf("%3s", rows[i]);
printf("\n\t%s\n", rows[i]);
}
return 0;
}
Or, you can allocate n
4-character arrays up front: 或者,您可以预先分配
n
4个字符的数组:
int main( int argc, char *argv[] )
{
char (*rows)[4];
int i, n;
printf("\nEnter the amount of rows in the telephone pad: ");
scanf("%d", &n);
rows = malloc( n * sizeof rows[0] );
printf("\nNow enter the configuration for the pad:\n");
for( i = 0; i < n; i++ ) {
scanf("%3s", rows[i]);
printf("\n\t%s\n", rows[i]);
}
return 0;
}
your printf is passing in a char, not a %s 您的printf传递的是字符而不是%s
you are saying get the i'th char in the string 'rows'. 您是说在字符串“ rows”中获取第i个字符。
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 我认为您需要一个字符串数组....或者您要将scanf更改为%c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.