2010-10-24 122 views
0

下面是我的代码为什么我收到一个警告“没有效果声明”?

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

使用'打破;'您的情况,请之间! – Benoit 2010-10-24 10:39:03

+0

洛尔好抓,完全忘了这一点。 – jon2512chua 2010-10-24 11:08:45

回答

12

因为!(options -> processHiddens)是一个表达式,而你并没有使用结果的任何东西。你需要的东西,如:

options->processHiddens = !options->processHiddens; 
+0

噢,丝毫没有察觉。一定是太累了。谢谢! – jon2512chua 2010-10-24 11:09:04

1

您的代码:

!(options -> processHiddens); 

丢弃的触发值,所以你得到的警告。你需要在变量的值切换回复制:

options -> processHiddens = ! options -> processHiddens; 
0

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

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

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

5

因为!(options -> processHiddens); “是一样的” 40 + 2。它真的有:-)

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

没有影响你想

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