开发者

Why am I getting a "statement with no effect" warning?

开发者 https://www.devze.com 2023-01-21 07:45 出处:网络
The following is my code /* Initialise default without options input. */ options -> processHiddens = false;

The following is my code

/* Initialise default without options input. */
options -> processHiddens = false;
options -> timeResolution = DEFAULT_MOD_TIMES;
options -> performSync = true;
options -&开发者_开发百科gt; 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;
    }
}

What I'm trying to do is to flip the boolean statement around each time an option is entered, hence doing something like

!(options -> processHiddens);

instead of just

options -> processHiddens = true;

However I'm getting the following warning when compiling:

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


Because !(options -> processHiddens) is an expression, and you're not assigning the result to anything. You need something like:

options->processHiddens = !options->processHiddens;


Because !(options -> processHiddens); "is the same as" 40 + 2. It really has no effect :-)

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

you want

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


Your code:

!(options -> processHiddens);

discards the toggled value, hence you get the warning. You need to copy the toggled value back in the variable:

options -> processHiddens = ! options -> processHiddens;


Regarding previous answer, I don't think options -> updateStatus is a function, because otherwise the compiler would complain with an error.

As for flipping the state, !(options -> updateStatus) is just a test (so to speak) to determine if options -> updateStatus is true or false.

What you need is this: options->updateStatus = !options->updateStatus

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号