views:

77

answers:

4

The following is my code

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

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
+7  A: 

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

options->processHiddens = !options->processHiddens;
Oli Charlesworth
Oh yeah, didn't even notice. Must be too tired. Thanks!
jon2512chua
+1  A: 

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;
codaddict
A: 

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

Tim Čas
+5  A: 

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