1

Slice of code:

#include <cs50.h>


void sort(int values[], int n)
{
    for (int i = 0, i < n, i++)

Error:

bubble.c:6:21: error: declaration shadows a local variable [-Werror,-Wshadow] for (int i = 0, i < n, i++)

There is more code but i assumed this would suffice since the error is at this point

Make File:

clang -fsanitize=integer -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow    bubble.c  -lcrypt -lcs50 -lm -o bubble

Full code:

#include <cs50.h>


void sort(int values[], int n)
{
    for (int i = 0, i < n, i++)
    {
        int min = i;
        {
            for (int j = i + 1, j < n + 1, j++)
            {
                if values[j] < values[i]
                {
                    min = j;
                }
                if j = n
                {
                    int hold = values[j];
                    values[j] = values[i];
                    values[i] = hold;
                }
            }
        }
    }
    return;
}

int main(void)
{
    int n = 7;
    int values[n] = {7, 6, 4, 2, 3, 1, 5};
    sort(values, n);
    printf(%i, values[]);
    return 0;
}
1

I was looking at your code seeing NO errors at all. I had to compile it myself to believe that I was going to get the same error, which I did. I've done this error several times before.

And you probably knew this already:

Structure of a for loop

for(initialize;condition;change some variable) { statement; }

You separated your three parts in the for loops with commas instead of semicolons. Lines 6 and 10.

Sometimes in code there is no way around looking for a semicolon for hours. That will give you the experience. And whenever you cannot find it, just give it to another pair of eyes. : )

2
  • you legend, thank you so much – Bijan Khadembashi Oct 22 '16 at 2:11
  • Thanks to you Bijan! I like how you make side programs to practice : ) That will give you advantage on CS50. – Ricardo David Antonetti Oct 22 '16 at 9:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .