Search code examples
cfor-loopsegmentation-faultgcc-warning

In a C program, getting warning: "Statement with no effect"


When I am trying to compile a particular program with -Wall, GCC is showing the warning as:

expcal.c:66:5: warning: statement with no effect [-Wunused-value]

this warning is referring the line:

ed.operator[j] == str[i];

which is found in the following loop:

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++;
                        }
                }
        }

I know that this warning will arise when there's something wrong with an assignment statement. What is wrong in the above code that would cause GCC to generate such a warning?


Solution

  • The statement

    ed.operator[j] == str[i];
    

    isn't an assignment; it's a comparison using ==. To make this an assignment, try writing

    ed.operator[j] = str[i];
    

    The warning that you're getting means that the comparison produces a value that's never used, so the statement doesn't have any visible effect on the program. It's a hint that you probably want to rewrite it as something with a side-effect.

    Hope this helps!