警告:格式 %s 需要 char * 类型,但参数 2 具有 int 类型

发布于 2024-12-19 11:43:31 字数 973 浏览 5 评论 0原文

我已经看过其他相关问题,但没有一个对这个案例有帮助。
我收到问题标题中列出的警告,我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把回忆走一遍 2024-12-26 11:43:31

rows[i] 不是 char* ——它不是“字符串”。

(并且一个字符中不能容纳 3 个字符(加上空终止符)。)

rows[i] isn't a char* -- it's not a "string".

(And you can't fit 3 characters (plus null terminator) in one character.)

紧拥背影 2024-12-26 11:43:31

正如其他人指出的那样,您正在为 n 个字符分配空间,但您确实需要为 n 行分配空间,每行 4 个字符(用户输入的 3 个字符,以及空终止符)。

有几种方法可以做到这一点。您可以首先分配 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;
}

或者,您可以分配 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;
}

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).

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:

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:

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;
}
勿忘心安 2024-12-26 11:43:31

的 %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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文