error in c: declaration shadows a variable in the global scope
up vote
0
down vote
favorite
When I try to compile the following code I get this error message:
error: declaration shadows a variable in the global scope:
void iterator(node* root)
I don't understand where exactly I'm hiding or shadowing the global variable I've declared before.
How can I fix this?
// typedef node
typedef struct node
bool is_word;
struct node* children[27];
node;
node* root = NULL;
void iterator(node* root)
for(int i = 0; i < 27; i++)
if (root -> children[i] != NULL)
iterator(root -> children[i]);
free(root);
return;
c global-variables
New contributor
add a comment |
up vote
0
down vote
favorite
When I try to compile the following code I get this error message:
error: declaration shadows a variable in the global scope:
void iterator(node* root)
I don't understand where exactly I'm hiding or shadowing the global variable I've declared before.
How can I fix this?
// typedef node
typedef struct node
bool is_word;
struct node* children[27];
node;
node* root = NULL;
void iterator(node* root)
for(int i = 0; i < 27; i++)
if (root -> children[i] != NULL)
iterator(root -> children[i]);
free(root);
return;
c global-variables
New contributor
1
Possible duplicate of warning: declaration of 'index' shadows a global declaration
– Scheff
Nov 10 at 11:22
What compiler and options are you using ?
– PilouPili
Nov 10 at 11:29
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
When I try to compile the following code I get this error message:
error: declaration shadows a variable in the global scope:
void iterator(node* root)
I don't understand where exactly I'm hiding or shadowing the global variable I've declared before.
How can I fix this?
// typedef node
typedef struct node
bool is_word;
struct node* children[27];
node;
node* root = NULL;
void iterator(node* root)
for(int i = 0; i < 27; i++)
if (root -> children[i] != NULL)
iterator(root -> children[i]);
free(root);
return;
c global-variables
New contributor
When I try to compile the following code I get this error message:
error: declaration shadows a variable in the global scope:
void iterator(node* root)
I don't understand where exactly I'm hiding or shadowing the global variable I've declared before.
How can I fix this?
// typedef node
typedef struct node
bool is_word;
struct node* children[27];
node;
node* root = NULL;
void iterator(node* root)
for(int i = 0; i < 27; i++)
if (root -> children[i] != NULL)
iterator(root -> children[i]);
free(root);
return;
c global-variables
c global-variables
New contributor
New contributor
New contributor
asked Nov 10 at 11:12
TheHummel
1
1
New contributor
New contributor
1
Possible duplicate of warning: declaration of 'index' shadows a global declaration
– Scheff
Nov 10 at 11:22
What compiler and options are you using ?
– PilouPili
Nov 10 at 11:29
add a comment |
1
Possible duplicate of warning: declaration of 'index' shadows a global declaration
– Scheff
Nov 10 at 11:22
What compiler and options are you using ?
– PilouPili
Nov 10 at 11:29
1
1
Possible duplicate of warning: declaration of 'index' shadows a global declaration
– Scheff
Nov 10 at 11:22
Possible duplicate of warning: declaration of 'index' shadows a global declaration
– Scheff
Nov 10 at 11:22
What compiler and options are you using ?
– PilouPili
Nov 10 at 11:29
What compiler and options are you using ?
– PilouPili
Nov 10 at 11:29
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
The compiler is sloppy in its error message; “global scope” is not something defined in the C standard. What it is trying to tell you is:
node* root = NULL;
declares root
as an identifier at file scope (it is visible from its declaration through the end of the translation unit [the source file being compiled]), and:
void iterator(node *root)
declares root
as an identifier at block scope (it is visible from its declaration through the end of the block that defines the function).
These declarations refer to two different objects. The first one is an object with static storage duration—it exists as long as your program is executing. The second one is a function parameter—it exists only while the function is executing, and there is a separate instance of it each time your function is called.
Inside the function, root
refers only to the function parameter. The former declaration is hidden and cannot be referred to by its name by any code inside the function. (That is another bit of sloppiness in the compiler error message; the C standard uses “hide,” not “shadow.”)
There is nothing wrong with this in regard to the C standard—you are allowed to hide identifiers. However, in regard to humans, it can cause problems because a person may write root
in one place intended it to refer to the root
in another place, because they did not see or forgot about the second declaration. This is why a compiler may have an optional warning about this. It appears you are compiling with that warning enabled, and with an option to elevate warnings into errors.
To fix it, you should either use different names for the static object and the function parameter or should turn off the compiler warning for hiding identifiers, whichever you think suits your project.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
The compiler is sloppy in its error message; “global scope” is not something defined in the C standard. What it is trying to tell you is:
node* root = NULL;
declares root
as an identifier at file scope (it is visible from its declaration through the end of the translation unit [the source file being compiled]), and:
void iterator(node *root)
declares root
as an identifier at block scope (it is visible from its declaration through the end of the block that defines the function).
These declarations refer to two different objects. The first one is an object with static storage duration—it exists as long as your program is executing. The second one is a function parameter—it exists only while the function is executing, and there is a separate instance of it each time your function is called.
Inside the function, root
refers only to the function parameter. The former declaration is hidden and cannot be referred to by its name by any code inside the function. (That is another bit of sloppiness in the compiler error message; the C standard uses “hide,” not “shadow.”)
There is nothing wrong with this in regard to the C standard—you are allowed to hide identifiers. However, in regard to humans, it can cause problems because a person may write root
in one place intended it to refer to the root
in another place, because they did not see or forgot about the second declaration. This is why a compiler may have an optional warning about this. It appears you are compiling with that warning enabled, and with an option to elevate warnings into errors.
To fix it, you should either use different names for the static object and the function parameter or should turn off the compiler warning for hiding identifiers, whichever you think suits your project.
add a comment |
up vote
1
down vote
The compiler is sloppy in its error message; “global scope” is not something defined in the C standard. What it is trying to tell you is:
node* root = NULL;
declares root
as an identifier at file scope (it is visible from its declaration through the end of the translation unit [the source file being compiled]), and:
void iterator(node *root)
declares root
as an identifier at block scope (it is visible from its declaration through the end of the block that defines the function).
These declarations refer to two different objects. The first one is an object with static storage duration—it exists as long as your program is executing. The second one is a function parameter—it exists only while the function is executing, and there is a separate instance of it each time your function is called.
Inside the function, root
refers only to the function parameter. The former declaration is hidden and cannot be referred to by its name by any code inside the function. (That is another bit of sloppiness in the compiler error message; the C standard uses “hide,” not “shadow.”)
There is nothing wrong with this in regard to the C standard—you are allowed to hide identifiers. However, in regard to humans, it can cause problems because a person may write root
in one place intended it to refer to the root
in another place, because they did not see or forgot about the second declaration. This is why a compiler may have an optional warning about this. It appears you are compiling with that warning enabled, and with an option to elevate warnings into errors.
To fix it, you should either use different names for the static object and the function parameter or should turn off the compiler warning for hiding identifiers, whichever you think suits your project.
add a comment |
up vote
1
down vote
up vote
1
down vote
The compiler is sloppy in its error message; “global scope” is not something defined in the C standard. What it is trying to tell you is:
node* root = NULL;
declares root
as an identifier at file scope (it is visible from its declaration through the end of the translation unit [the source file being compiled]), and:
void iterator(node *root)
declares root
as an identifier at block scope (it is visible from its declaration through the end of the block that defines the function).
These declarations refer to two different objects. The first one is an object with static storage duration—it exists as long as your program is executing. The second one is a function parameter—it exists only while the function is executing, and there is a separate instance of it each time your function is called.
Inside the function, root
refers only to the function parameter. The former declaration is hidden and cannot be referred to by its name by any code inside the function. (That is another bit of sloppiness in the compiler error message; the C standard uses “hide,” not “shadow.”)
There is nothing wrong with this in regard to the C standard—you are allowed to hide identifiers. However, in regard to humans, it can cause problems because a person may write root
in one place intended it to refer to the root
in another place, because they did not see or forgot about the second declaration. This is why a compiler may have an optional warning about this. It appears you are compiling with that warning enabled, and with an option to elevate warnings into errors.
To fix it, you should either use different names for the static object and the function parameter or should turn off the compiler warning for hiding identifiers, whichever you think suits your project.
The compiler is sloppy in its error message; “global scope” is not something defined in the C standard. What it is trying to tell you is:
node* root = NULL;
declares root
as an identifier at file scope (it is visible from its declaration through the end of the translation unit [the source file being compiled]), and:
void iterator(node *root)
declares root
as an identifier at block scope (it is visible from its declaration through the end of the block that defines the function).
These declarations refer to two different objects. The first one is an object with static storage duration—it exists as long as your program is executing. The second one is a function parameter—it exists only while the function is executing, and there is a separate instance of it each time your function is called.
Inside the function, root
refers only to the function parameter. The former declaration is hidden and cannot be referred to by its name by any code inside the function. (That is another bit of sloppiness in the compiler error message; the C standard uses “hide,” not “shadow.”)
There is nothing wrong with this in regard to the C standard—you are allowed to hide identifiers. However, in regard to humans, it can cause problems because a person may write root
in one place intended it to refer to the root
in another place, because they did not see or forgot about the second declaration. This is why a compiler may have an optional warning about this. It appears you are compiling with that warning enabled, and with an option to elevate warnings into errors.
To fix it, you should either use different names for the static object and the function parameter or should turn off the compiler warning for hiding identifiers, whichever you think suits your project.
edited Nov 10 at 11:35
answered Nov 10 at 11:30
Eric Postpischil
68.8k873149
68.8k873149
add a comment |
add a comment |
TheHummel is a new contributor. Be nice, and check out our Code of Conduct.
TheHummel is a new contributor. Be nice, and check out our Code of Conduct.
TheHummel is a new contributor. Be nice, and check out our Code of Conduct.
TheHummel is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238366%2ferror-in-c-declaration-shadows-a-variable-in-the-global-scope%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Possible duplicate of warning: declaration of 'index' shadows a global declaration
– Scheff
Nov 10 at 11:22
What compiler and options are you using ?
– PilouPili
Nov 10 at 11:29