简体   繁体   English

警告:格式%s期望为char *类型,但参数2为int类型

[英]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.cgcc -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.

相关问题 警告:格式&#39;%s&#39;需要类型&#39;char *&#39;,但参数2的类型为&#39;int&#39; - warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’ 警告:格式&#39;%c&#39;需要类型&#39;int&#39;,但参数2的类型为&#39;char *&#39; - warning: format '%c' expects type 'int', but argument 2 has type 'char *' 警告:格式“%s”需要类型“char *”,但参数 2 的类型为“char (*)” - warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)’ 警告:格式&#39;%s&#39;期望参数类型为&#39;char *&#39;,但是参数2使用argv类型为&#39;int&#39; - warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ using argv 格式为&#39;%s&#39;,参数类型为&#39;char *&#39;,但是参数2为类型&#39;int&#39; - format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ 格式&#39;%s&#39;期望参数类型为&#39;* char&#39;,但参数2的类型为&#39;int&#39; - format '%s' expects argument of type '*char' but argument 2 has type 'int' C-“警告:格式”%c”期望参数类型为“ char *”,但是参数3的类型类型为“ int”: - C - “warning: format ”%c“ expects argument of type ”char *“, but argument 3 has type ”int":? GCC编译器警告:格式&#39;%c&#39;需要类型为&#39;char *&#39;的参数,但参数2为类型&#39;int *&#39;[-Wformat] - GCC compiler warning: format ‘%c’ expects argument of type ‘char *’, but argument 2 has type ‘int *’ [-Wformat] 警告:格式“c”需要“int”类型的参数,但参数 2 的类型为“char” - warning :format 'c' expects argument of type 'int', but argument 2 has type 'char ' 警告:格式“%d”需要“int *”类型的参数,但参数 3 的类型为“uint8_t * {aka unsigned char *} - warning: format ‘%d’ expects argument of type ‘int *’, but argument 3 has type ‘uint8_t * {aka unsigned char *}
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM