Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why do I get "error: variable 'sum_r' set but not used [-Werror,-Wunused-but-set-variable]" when I set a variable inside a for loop?

Why do I get an error, error: variable 'sum_r' set but not used [-Werror,-Wunused-but-set-variable] when I set the variables sum_r sum_g, and sum_b in a for loop then try to change their values later in another for loop?

Here is my current code:

// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
    RGBTRIPLE new[height][width];

    for (int i = 0; i < height; i++)
    {
        for (int j = 0; j < width; j++)
        {
            int sum_r = 0;
            int sum_g = 0;
            int sum_b = 0;

            for (int ji = i - 1; ji < 3; ji++)
            {
                for (int jj = j - 1; jj < 3; jj++)
                {
                    int rgb_r = image[ji][jj].rgbtRed;
                    int rgb_g = image[ji][jj].rgbtGreen;
                    int rgb_b = image[ji][jj].rgbtBlue;

                    sum_r += rgb_r;
                    sum_g += rgb_g;
                    sum_b += rgb_b;
                }
            }
        }
    }
    return;
}

I have tried using pointers and addresses but I cannot seem to figure it out. Could someone explain why this is happening?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

  1. You do not need return in a void function
  2. You are getting this message because you set those variables in the loop but you do not use it. Your function can be reduced to the no-op and the compiler is warning that you forgot to do something with your value.

optimizing compilers will rescue this function to:

void blur(int height, int width, RGBTRIPLE image[height][width])
{
}

https://godbolt.org/z/cEeejGKrh

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading