0

I'm a little confused as to the compiler warnings that I am getting and how to resolve them. Here are the errors and the relevant code slices:

declaration of cmds (relevant to most of them):

 23: static char **cmds[] = { cmd0, cmd1, cmd2, cmd3, cmd4 }; 24: static int ncmds = sizeof(cmds) / sizeof(cmds[0]); pipeline.c: In function âexec_nth_commandâ: pipeline.c:41: warning: declaration of âncmdsâ shadows a global declaration pipeline.c:24: warning: shadowed declaration is here pipeline.c:41: warning: declaration of âcmdsâ shadows a global declaration pipeline.c:23: warning: shadowed declaration is here 41: static void exec_nth_command(int ncmds, char ***cmds) pipeline.c: In function âexec_pipe_commandâ: pipeline.c:68: warning: declaration of âncmdsâ shadows a global declaration pipeline.c:24: warning: shadowed declaration is here pipeline.c:68: warning: declaration of âcmdsâ shadows a global declaration pipeline.c:23: warning: shadowed declaration is here 68: static void exec_pipe_command(int ncmds, char ***cmds, Pipe output) pipeline.c: In function âexec_pipelineâ: pipeline.c:79: warning: declaration of âncmdsâ shadows a global declaration pipeline.c:24: warning: shadowed declaration is here pipeline.c:79: warning: declaration of âcmdsâ shadows a global declaration pipeline.c:23: warning: shadowed declaration is here 79: static void exec_pipeline(int ncmds, char ***cmds) pipeline.c:82: warning: ISO C90 forbids mixed declarations and code 82: pid_t pid; pipeline.c: In function âerr_usageâ: pipeline.c:141: warning: declaration of âusestrâ shadows a global declaration pipeline.c:26: warning: shadowed declaration is here 26: static char const usestr[] = "[-f filename]"; 141: static void err_usage(char const *usestr) 

1 Answer 1

1

The warnings are saying your local names cover up other variables with the same name. So

pipeline.c:41: warning: declaration of âncmdsâ shadows a global declaration 

the ncmds parameter is hidden by the static char **cmds[] = variable, making the parameter inaccessible.

The way to resolve these is to simply choose a different name for the param or for the variable. I'd change the name of the one you use less often, so you don't have to change as much code. You could also just ignore the warning, but the reason it is there is that when looking at the code later, or writing new things in that function, you might accidentally refer to one when you mean the other, since the same name can be confusing.

pipeline.c:82: warning: ISO C90 forbids mixed declarations and code 

This one is because in older style C, declaring a variable anywhere except the top of the scope is not allowed. There's no problem doing it in practice though, in fact, it is generally better to declare the variable as close as possible to the usage point, so I wouldn't change the code here. Instead, try compiling with -std=c99 if you can - the newer version of the standard does allow it.

Sign up to request clarification or add additional context in comments.

5 Comments

I don't understand how two differently named variables of different types can hide one another.
doesn't the parameter hide the file scope variable, not the other way round?
@MarshallTigerus: because one is a global variable assigns the other is a local one.
Oh, the one within the function and the global. I get it now. I thought what was said was that the two similarly named globals were hiding one another, which didn't make sense.
Changing names cleared up most of this. Got one or two resulting errors that I have to work out still, but thank you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.