[英]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 -> updateStatus
是true
還是false
。
您需要的是: options->updateStatus = !options->updateStatus
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.