我已经查看了其他相关问题,但没有一个对这个案例有帮助。
我收到问题标题中列出的警告,我的 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 <stdio.h>
和 #include <stdlib.h>
。
注 4:我已经对程序进行了 gcc 编译: gcc -ansi -pedantic -Wall tele.c
。
感谢您的帮助。
最佳答案
rows[i]
不是 char*
——它不是“字符串”。
(并且一个字符中不能容纳 3 个字符(加上空终止符)。)
关于c - 警告 : format %s expects type char * but argument 2 has type int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8394468/