How to change value of variable passed as argument?

后端 未结 4 1795
天涯浪人
天涯浪人 2020-11-22 09:03

How to change value of variable passed as argument in C? I tried this:

void foo(char *foo, int baa){
    if(baa) {
        foo = \"ab\";
    } else {
                


        
4条回答
  •  逝去的感伤
    2020-11-22 09:27

    You question is titled in a generic way. You've already got answers for your specific problem. I'm going to add couple of illustrative examples for int and double types.

    #include 
    
    void incrementInt(int* in, int inc)
    {
       *in += inc;
    }
    
    void incrementDouble(double* in, double inc)
    {
       *in += inc;
    }
    
    int main()
    {
       int i = 10;
       double d = 10.2;
    
       printf("i: %d, d: %lf\n", i, d);
    
       incrementInt(&i, 20);
       incrementDouble(&d, 9.7);
    
       printf("i: %d, d: %lf\n", i, d);
    }
    

    Output:

    i: 10, d: 10.200000
    i: 30, d: 19.900000
    

提交回复
热议问题