Can I set a default argument from a previous argument?

后端 未结 7 1775
终归单人心
终归单人心 2020-11-27 20:09

Is it possible to use previous arguments in a functions parameter list as the default value for later arguments in the parameter list? For instance,

void f(         


        
7条回答
  •  失恋的感觉
    2020-11-27 20:22

    I do not think you can do that as that is an illegal syntax. But however, consult the C99 standard in pdf format (n1136.pdf).

    However, you may get around this by using static as in declaring the variables static and using them within the function f

    static int global_a;
    
    /* In some other spot where you are calling f(), do this beforehand */
    /* global_a = 4; f(); */
    
    void f(void){
       int a = global_a;
       b = c = a;
       /* ..... */
    }
    

    Kudos to Michael Burr for pointing out my error! :)

    It sounds like you need to rethink your code and change it around for something like that.

提交回复
热议问题