为什么我收到“没有效果的声明”警告?

时间:2010-10-24 10:37:47

标签: c structure compiler-warnings

以下是我的代码

/* 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

4 个答案:

答案 0 :(得分:12)

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

options->processHiddens = !options->processHiddens;

答案 1 :(得分:5)

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

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

你想要

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

答案 2 :(得分:1)

您的代码:

!(options -> processHiddens);

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

options -> processHiddens = ! options -> processHiddens;

答案 3 :(得分:0)

关于以前的回答,我认为options -> updateStatus不是函数,因为否则编译器会抱怨错误。

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

您需要的是:options->updateStatus = !options->updateStatus