在C程序中,收到警告:"声明无效"

Dha*_*eem 2 c for-loop segmentation-fault gcc-warning

当我尝试编译特定程序时-Wall,GCC显示警告:

expcal.c:66:5: warning: statement with no effect [-Wunused-value]
Run Code Online (Sandbox Code Playgroud)

这个警告是指线:

ed.operator[j] == str[i];
Run Code Online (Sandbox Code Playgroud)

在以下循环中找到:

for(i=0;i<strlen(str);i++)
        {
                j=0;
                if(str[i] == '+' || str[i] == '-' || str[i] == '*')
                {
                        if(str[i+1] == '+' || str[i+1] == '-' || str[i+1] == '*')
                                return 0;
                        else
                        {
                                //j=0;
                                ed.operator[j] == str[i];
                                count++;
                                j++;
                        }
                }
        }
Run Code Online (Sandbox Code Playgroud)

我知道当一个赋值语句出现问题时会出现这个警告.上面的代码有什么问题会导致GCC产生这样的警告?

tem*_*def 11

该声明

ed.operator[j] == str[i];
Run Code Online (Sandbox Code Playgroud)

不是任务; 这是一个比较使用==.为了完成这项任务,请尝试写作

ed.operator[j] = str[i];
Run Code Online (Sandbox Code Playgroud)

您获得的警告意味着比较产生了一个从未使用过的值,因此该语句对程序没有任何明显的影响.这是一个提示,你可能想把它重写为具有副作用的东西.

希望这可以帮助!

  • `examples_of_why_to_use_Wall ++` (4认同)