Functions that call each other, declared inside another function





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I have two functions that call each other, inside of another function:



int main(){
void aaa();

void b(){
aaa();
}

void aaa(){
b();
}

aaa();
}


(Yes, this example would be stuck in an infinite loop or cause a stack overflow)



The compiler throws the error: static declaration of 'aaa' follows non-static declaration



If I move the function declarations outside of the other function, it works (but I can't do that because those functions need to have access to main's local variables)



Is it possible to make this work?










share|improve this question


















  • 2





    You can't nest function definitions in Standard C. GNU C only supports it as an extension.

    – Jens
    Nov 16 '18 at 19:53











  • Why don't you just declare these functions outside the main as it should be, and then pass the main variables you need inside these functions as parameters?

    – Simone De Vita
    Nov 16 '18 at 19:59











  • Oh, I didn't know that wasn't part of the standard.

    – 12Me21
    Nov 16 '18 at 21:11


















0















I have two functions that call each other, inside of another function:



int main(){
void aaa();

void b(){
aaa();
}

void aaa(){
b();
}

aaa();
}


(Yes, this example would be stuck in an infinite loop or cause a stack overflow)



The compiler throws the error: static declaration of 'aaa' follows non-static declaration



If I move the function declarations outside of the other function, it works (but I can't do that because those functions need to have access to main's local variables)



Is it possible to make this work?










share|improve this question


















  • 2





    You can't nest function definitions in Standard C. GNU C only supports it as an extension.

    – Jens
    Nov 16 '18 at 19:53











  • Why don't you just declare these functions outside the main as it should be, and then pass the main variables you need inside these functions as parameters?

    – Simone De Vita
    Nov 16 '18 at 19:59











  • Oh, I didn't know that wasn't part of the standard.

    – 12Me21
    Nov 16 '18 at 21:11














0












0








0








I have two functions that call each other, inside of another function:



int main(){
void aaa();

void b(){
aaa();
}

void aaa(){
b();
}

aaa();
}


(Yes, this example would be stuck in an infinite loop or cause a stack overflow)



The compiler throws the error: static declaration of 'aaa' follows non-static declaration



If I move the function declarations outside of the other function, it works (but I can't do that because those functions need to have access to main's local variables)



Is it possible to make this work?










share|improve this question














I have two functions that call each other, inside of another function:



int main(){
void aaa();

void b(){
aaa();
}

void aaa(){
b();
}

aaa();
}


(Yes, this example would be stuck in an infinite loop or cause a stack overflow)



The compiler throws the error: static declaration of 'aaa' follows non-static declaration



If I move the function declarations outside of the other function, it works (but I can't do that because those functions need to have access to main's local variables)



Is it possible to make this work?







c function recursion






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 19:52









12Me2112Me21

209313




209313








  • 2





    You can't nest function definitions in Standard C. GNU C only supports it as an extension.

    – Jens
    Nov 16 '18 at 19:53











  • Why don't you just declare these functions outside the main as it should be, and then pass the main variables you need inside these functions as parameters?

    – Simone De Vita
    Nov 16 '18 at 19:59











  • Oh, I didn't know that wasn't part of the standard.

    – 12Me21
    Nov 16 '18 at 21:11














  • 2





    You can't nest function definitions in Standard C. GNU C only supports it as an extension.

    – Jens
    Nov 16 '18 at 19:53











  • Why don't you just declare these functions outside the main as it should be, and then pass the main variables you need inside these functions as parameters?

    – Simone De Vita
    Nov 16 '18 at 19:59











  • Oh, I didn't know that wasn't part of the standard.

    – 12Me21
    Nov 16 '18 at 21:11








2




2





You can't nest function definitions in Standard C. GNU C only supports it as an extension.

– Jens
Nov 16 '18 at 19:53





You can't nest function definitions in Standard C. GNU C only supports it as an extension.

– Jens
Nov 16 '18 at 19:53













Why don't you just declare these functions outside the main as it should be, and then pass the main variables you need inside these functions as parameters?

– Simone De Vita
Nov 16 '18 at 19:59





Why don't you just declare these functions outside the main as it should be, and then pass the main variables you need inside these functions as parameters?

– Simone De Vita
Nov 16 '18 at 19:59













Oh, I didn't know that wasn't part of the standard.

– 12Me21
Nov 16 '18 at 21:11





Oh, I didn't know that wasn't part of the standard.

– 12Me21
Nov 16 '18 at 21:11












1 Answer
1






active

oldest

votes


















0














C does not support nested functions, nor closures.



One solution for your actual problem is to define the shared state (the local variables of main you mentioned, in this case) as struct, then have variable of this struct type, and pass it as pointer to the functions that use it.



You don't show the local variable code in your question, so I'm not writing and arbitrary example. Please edit the question with more code, if you want an example.





Another solution is to just use global (preferably static) variables, used both from other functions and main. I mean, since main isn't a normal function (can't be called recursively by standard, for example), its local variables end up being unique objects anyway, so this is just a matter of scope and visibility, with little to no funcional difference.






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',
    autoActivateHeartbeat: false,
    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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53344493%2ffunctions-that-call-each-other-declared-inside-another-function%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    C does not support nested functions, nor closures.



    One solution for your actual problem is to define the shared state (the local variables of main you mentioned, in this case) as struct, then have variable of this struct type, and pass it as pointer to the functions that use it.



    You don't show the local variable code in your question, so I'm not writing and arbitrary example. Please edit the question with more code, if you want an example.





    Another solution is to just use global (preferably static) variables, used both from other functions and main. I mean, since main isn't a normal function (can't be called recursively by standard, for example), its local variables end up being unique objects anyway, so this is just a matter of scope and visibility, with little to no funcional difference.






    share|improve this answer






























      0














      C does not support nested functions, nor closures.



      One solution for your actual problem is to define the shared state (the local variables of main you mentioned, in this case) as struct, then have variable of this struct type, and pass it as pointer to the functions that use it.



      You don't show the local variable code in your question, so I'm not writing and arbitrary example. Please edit the question with more code, if you want an example.





      Another solution is to just use global (preferably static) variables, used both from other functions and main. I mean, since main isn't a normal function (can't be called recursively by standard, for example), its local variables end up being unique objects anyway, so this is just a matter of scope and visibility, with little to no funcional difference.






      share|improve this answer




























        0












        0








        0







        C does not support nested functions, nor closures.



        One solution for your actual problem is to define the shared state (the local variables of main you mentioned, in this case) as struct, then have variable of this struct type, and pass it as pointer to the functions that use it.



        You don't show the local variable code in your question, so I'm not writing and arbitrary example. Please edit the question with more code, if you want an example.





        Another solution is to just use global (preferably static) variables, used both from other functions and main. I mean, since main isn't a normal function (can't be called recursively by standard, for example), its local variables end up being unique objects anyway, so this is just a matter of scope and visibility, with little to no funcional difference.






        share|improve this answer















        C does not support nested functions, nor closures.



        One solution for your actual problem is to define the shared state (the local variables of main you mentioned, in this case) as struct, then have variable of this struct type, and pass it as pointer to the functions that use it.



        You don't show the local variable code in your question, so I'm not writing and arbitrary example. Please edit the question with more code, if you want an example.





        Another solution is to just use global (preferably static) variables, used both from other functions and main. I mean, since main isn't a normal function (can't be called recursively by standard, for example), its local variables end up being unique objects anyway, so this is just a matter of scope and visibility, with little to no funcional difference.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 16 '18 at 20:07

























        answered Nov 16 '18 at 20:00









        hydehyde

        32.2k1589133




        32.2k1589133
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53344493%2ffunctions-that-call-each-other-declared-inside-another-function%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine