Benefits of pointers?

后端 未结 11 1590
不知归路
不知归路 2021-01-03 23:40

I recently developed an interest in C programming so I got myself a book (K&R) and started studying.

Coming from a University course in Java (basics), pointers a

11条回答
  •  天涯浪人
    2021-01-04 00:22

    In this example:

    int f1(int i); 
    f1(x);
    

    the parameter i is passed by value, so the function f1 could not change the value of variable x of the caller.

    But in this case:

    int f2(int* i);
    int x;
    int* px = &x;
    f2(px);
    

    Here we still pass px parameter by value, but in the same time we pass x by refference!. So if the callee (f2) will change its int* i it will have no effect on px in the caller. However, by changing *i the callee will change the value of x in the caller.

提交回复
热议问题