繁体   English   中英

为什么会收到“无效声明”警告?

[英]Why am I getting a “statement with no effect” warning?

以下是我的代码

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

因为!(options -> processHiddens)是一个表达式,所以您没有将结果分配给任何东西。 您需要类似:

options->processHiddens = !options->processHiddens;

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

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

你要

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

您的代码:

!(options -> processHiddens);

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

options -> processHiddens = ! options -> processHiddens;

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM