【问题标题】:I compiled this program and the compiler keeps giving me this error我编译了这个程序,编译器一直给我这个错误
【发布时间】:2022-01-20 08:39:28
【问题描述】:
#include <stdio.h>
#include <ctype.h>

int main(void) {
  int j;
  scanf ("%d",&j);
  if (j>=1&&j<=10){
    int i=0;
    int USER_NAME=100;
    char name[j][USER_NAME];
    for (i=0;i<j;i++){
      scanf ("%s",name[i]);
    }
    for (i=0;i<j;i++){
      if ((i%2)==0){
        i++;
      }
      if ((i%2)!=0){
        printf ("%s\n",name[i]);
      }
    }

  }
  else (printf ("No additional constrainsts"));
  return 0;
}

它一直给我这个错误

./oddecho.c: In function 'main':
./oddecho.c:6:3: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
    6 |   scanf ("%d",&j);
      |   ^~~~~~~~~~~~~~~
./oddecho.c:12:7: warning: ignoring return value of 'scanf', declared with attribute warn_unused_result [-Wunused-result]
   12 |       scanf ("%s",name[i]);
      |       ^~~~~~~~~~~~~~~~~~~~

我尝试在第 6 行的 scanf 之前使用 (void),但它仍然不断出现问题。 有人可以帮助为什么这个错误不断弹出? 任何帮助将不胜感激

【问题讨论】:

    标签: c for-loop warnings


    【解决方案1】:

    我在您的代码中发现了一些问题。

    1. 您正在使用 non-const 变量来分配静态数组。 IDK 你的编译器如何编译它而不给出jUSER_NAME 不是常量的错误。我建议改用此代码:
    ...
        // Manual allocate memories
        char** names = (char**)malloc(j * sizeof(char*));
        for (i = 0; i < j; i++) {
          names[i] = (char*)malloc(USER_NAME * sizeof(char));
          scanf("%s", names[i]);
        }
    ...
        // Free memories
        for (i = 0; i < j; i++) {
          free(names[i]);
        }
        free(names);
    ...
    
    1. 如果您遇到由 MSVC 编译器导致的错误 error C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.,您可以将此行放在包含标头 #define _CRT_SECURE_NO_WARNINGS 之前,或查看此参考 https://docs.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4996?view=msvc-170 了解更多详细信息。
    2. 关于警告。 scanf 函数的签名是int scanf ( const char * format, ... ),这意味着它返回一个整数值。在不存储从它返回的值的情况下使用它可能会导致该警告。

    【讨论】:

    • 1. You are using non-const variable to allocate a static array - 这个数组不是静态的,它是自动的。并阅读 VLAs 2。很明显 OP 不使用 MSVC 编译器
    • 如果编译器支持 VLA 那就错了
    • 感谢您提供的有用信息。
    • #define ARR_SIZE 100const int ARR_SIZE = 100 非常不同,如果不支持 VLA,则第二个不能用于数组初始化。我建议你删除你的评论,因为它是不正确的
    【解决方案2】:

    一般情况下你需要检查scanf的返回值来确保输入成功。

    并且警告说明了这一点。

    为避免出现警告,您可以将带有 scanf 的表达式转换为 void 类型

    ( void )scanf ("%d",&j);
    

    注意,由于在这个for循环中变量i增加了两次

    for (i=0;i<j;i++){
      if ((i%2)==0){
        i++;
      }
      if ((i%2)!=0){
        printf ("%s\n",name[i]);
      }
    }
    

    什么都输出不了。:)

    随便写

    for (i=0;i<j;i++){
      if ((i%2)!=0){
        printf ("%s\n",name[i]);
        i++;
      }
    }
    

    【讨论】:

    • 使用 gcc 9.3.0 和 -O3 -Wall -Wextra -Wpedantic 甚至没有 (void) 使警告静音:
    猜你喜欢
    • 1970-01-01
    • 2013-03-03
    • 2020-09-16
    • 1970-01-01
    • 2019-09-18
    • 1970-01-01
    • 2018-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多