【问题标题】:Why am I getting a "statement with no effect" warning?为什么我会收到“无效声明”警告?
【发布时间】:2010-10-24 10:37:46
【问题描述】:

以下是我的代码

/* Initialise default without options input. */
options -> processHiddens = false;
options -> timeResolution = DEFAULT_MOD_TIMES;
options -> performSync = true;
options -> recursive = false;
options -> print = false;
options -> updateStatus = true;
options -> verbose = false;
options -> programname = malloc(BUFSIZ);
options -> programname = argv[0];

while ((opt = getopt(argc, argv, OPTLIST)) != -1)
{
    switch (opt)
    {
        case 'a':
            !(options -> processHiddens);
        case 'm':
            options -> timeResolution = atoi(optarg);
        case 'n':
            !(options -> performSync);
        case 'p':
            !(options -> print);
        case 'r':
            !(options -> recursive);
        case 'u':
            !(options -> updateStatus);
        case 'v':
            !(options -> verbose);
        default:
            argc = -1;
    }
}

我要做的是在每次输入选项时翻转布尔语句,因此执行类似

!(options -> processHiddens);

而不仅仅是

options -> processHiddens = true;

但是我在编译时收到以下警告:

mysync.c: In function ‘main’:
mysync.c:32: warning: statement with no effect
mysync.c:36: warning: statement with no effect
mysync.c:38: warning: statement with no effect
mysync.c:40: warning: statement with no effect
mysync.c:42: warning: statement with no effect
mysync.c:44: warning: statement with no effect

【问题讨论】:

  • 请在您的案例之间使用break;
  • 哈哈,很好,完全忘记了。

标签: c structure compiler-warnings


【解决方案1】:

因为!(options -> processHiddens) 是一个表达式,并且您没有将结果分配给任何东西。你需要这样的东西:

options->processHiddens = !options->processHiddens;

【讨论】:

  • 哦,是的,甚至没有注意到。一定是太累了。谢谢!
【解决方案2】:

因为!(options -> processHiddens);“与”40 + 2 相同。它真的没有效果:-)

printf("foo");
40 + 2;
printf("bar");

你想要的

option -> processHiddens = !(options -> processHiddens);
break;        /* without break, all the following lines will execute */

【讨论】:

    【解决方案3】:

    您的代码:

    !(options -> processHiddens);
    

    丢弃切换的值,因此您会收到警告。您需要将切换后的值复制回变量中:

    options -> processHiddens = ! options -> processHiddens;
    

    【讨论】:

      【解决方案4】:

      关于之前的回答,我不认为options -> updateStatus 是一个函数,否则编译器会报错。

      至于翻转状态,!(options -> updateStatus) 只是一个测试(可以这么说),以确定options -> updateStatustrue 还是false

      你需要的是这个:options->updateStatus = !options->updateStatus

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-10
        相关资源
        最近更新 更多