'buf' shadows a global declaration

Posted by Miked on Wed 13 Jul 2005 10:07 AM — 12 posts, 45,207 views.

USA #0
I get an error saying that 'buf' shadows a global declaration, but I go to take it out and I get more errors. For example, I take it out of do_who in act_info.c of SWFotE 1.2, and I get errors in passing stuff from incompatable pointer type. Any suggestions, or do you need more info?
Australia #1
As long as you take out the shadowed declaration all should be fine.

void do_nothing(*argument)
char declare[MIL]

if (something and something)
  char declare[MIL] //remove this one


Well my example might not be very pretty or very acurate, but you should remove where the variable has been declared for the second time within that function.

Now, there are other more sinister shaddowed declarations, ones that deal with librarys and such, some more detail would be needed to see what exaclty you have going on.
Canada #2
This issue generally means that you have a variable called "buf" declared somewhere as a global variable(usually) and that the compiler is complaining because it is not obvious which one to use inside the function: the local one, or the global one. Since "buf" is very common as a local variable, I wouls likely change the global one.
USA #3
*nod* A common global def would be "log_buf" but I think buf is too common to be used as a global def.
USA #4
I didn't realize shadowing is an error-inducing problem (although it can make for very hard-to-track bugs...) What the compiler is supposed to do is use the local-most variable - nice compilers emit warnings, not errors, about shadowing. This might be a C++ vs C thing, though; I haven't coded in straight C for a while and have no reason to. :-)
Canada #5
I beleive that it is produced as an error by the pedantic flag, but I could be wrong.
USA #6
Even so, when I compile I use the highest warning levels, pedantic etc., and I've never gotten that error.

I just wrote the following:
Quote:

#include <stdio.h>

int i = 3;

int main()
{
    int i = 4;

    printf("Hello there: %d\n", i);

    return 0;
}

And tested it:
dhaley@muri5 ~/tmp
$ gcc -Wall --pedantic test.c -o test

dhaley@muri5 ~/tmp
$ ./test
Hello there: 4

dhaley@muri5 ~/tmp
$ 
As expected, we get 4. Didn't even get a warning about a shadowing... strange. Note that it's all C, not C++, for what it's worth.
#7
Quote:
Even so, when I compile I use the highest warning levels, pedantic etc., and I've never gotten that error.


-Wall does not warn about shadowing. You need to turn on -Wshadow to do that.
Amended on Thu 14 Jul 2005 03:21 PM by Raz
USA #8
Even so, that is a warning, not an error, so I'm not sure where this error is coming from. Unless of course the OP really meant warning, not error.
Canada #9
Could have the flag that turns warning into errors on. Can't remember what it is off the top of my head, though.

[edit]

I think it's -Werror
Amended on Thu 14 Jul 2005 07:49 PM by Greven
Australia #10
With -Werror it will not compile with a warning.
Amended on Fri 15 Jul 2005 12:33 AM by Robert Powell
USA #11
Right... -Werror makes it treat warnings as errors, like Greven said. :)