Shadowed declaration

Posted by Metsuro on Sat 24 Dec 2005 11:03 PM — 3 posts, 20,312 views.

USA #0
hometowns.c: In function `load_nations':
hometowns.c:328: warning: declaration of 'filename' shadows a previous local
hometowns.c:314: warning: shadowed declaration is here

{
FILE *list = NULL;
char filename[256]; - 314

/* load from nations list */
sprintf(filename, "%snations.lst", NATION_DIR);
if((list = fopen(filename, "r")) == NULL)
{
bug("load_nations(): cannot open %s for reading; aborting load", filename);
return;
}

top_nation = 0;

for( ; ; )
{
char *filename = feof(list) ? "$" : fread_string(list); - 328

not sure how to fix this or anything...
USA #1
Try doing a search on the forums. For example, this comes up:
http://www.gammon.com.au/forum/bbshowpost.php?bbsubject_id=5703
USA #2
That's because the "filename" variable is being declared twice. A shadow warning lets you know this was done since in some cases it can fubar the contents of the variable if you do that.

You should probably change the "filename" variable inside that for() loop to something else. I suggest "fname". This will avoid the possibility that you might end up overwriting something in memory that you don't want to.

( This also means I need to go back and check this code for errors since I'm the one hosting it :P )