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;










share|improve this question







New contributor




TheHummel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    Possible duplicate of warning: declaration of 'index' shadows a global declaration
    – Scheff
    2 days ago










  • What compiler and options are you using ?
    – PilouPili
    2 days ago














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;










share|improve this question







New contributor




TheHummel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1




    Possible duplicate of warning: declaration of 'index' shadows a global declaration
    – Scheff
    2 days ago










  • What compiler and options are you using ?
    – PilouPili
    2 days ago












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;










share|improve this question







New contributor




TheHummel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











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






share|improve this question







New contributor




TheHummel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question







New contributor




TheHummel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question






New contributor




TheHummel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









TheHummel

1




1




New contributor




TheHummel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





TheHummel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






TheHummel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1




    Possible duplicate of warning: declaration of 'index' shadows a global declaration
    – Scheff
    2 days ago










  • What compiler and options are you using ?
    – PilouPili
    2 days ago












  • 1




    Possible duplicate of warning: declaration of 'index' shadows a global declaration
    – Scheff
    2 days ago










  • What compiler and options are you using ?
    – PilouPili
    2 days ago







1




1




Possible duplicate of warning: declaration of 'index' shadows a global declaration
– Scheff
2 days ago




Possible duplicate of warning: declaration of 'index' shadows a global declaration
– Scheff
2 days ago












What compiler and options are you using ?
– PilouPili
2 days ago




What compiler and options are you using ?
– PilouPili
2 days ago












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.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );






    TheHummel is a new contributor. Be nice, and check out our Code of Conduct.









     

    draft saved


    draft discarded


















    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






























    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.






    share|improve this answer


























      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.






      share|improve this answer
























        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 2 days ago

























        answered 2 days ago









        Eric Postpischil

        68.7k873149




        68.7k873149




















            TheHummel is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            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.













             


            draft saved


            draft discarded














            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














































































            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党