0

How come only when its passed by reference does the value of age change? why doesn't it work when passed by value?

    #include<stdio.h>
struct employee{
char name[20];
int age;
};
void modify_byVal(struct employee v){
    v.age=v.age+2;
    }
    void modify_byRef(struct employee *p){
 p ->age=p->age+2;
 }
 void main(){
     struct employee Sam = {"Sam", 35};
     struct employee Mary = {"Mary", 25};
     modify_byVal(Sam);
     modify_byRef(&Mary);
     printf("%s %d", Sam.name, Sam.age);
     printf(" ");
     printf("%s %d", Mary.name, Mary.age);
     }

Output of code

Alex
  • 13
  • 3
  • 1
    C doesn't support pass by reference – Lalit Verma Dec 05 '17 at 17:56
  • @LalitVerma - C may not "support" it, but it's possible, as this post demonstrates. Pass-by-reference and pass-by-pointer are, at the most basic level, the same thing. They differ only in concept. – phonetagger Dec 05 '17 at 18:07
  • 1
    @phonetagger these are not same thing , passing pointer is just like passing value, difference is you just pass address. – Lalit Verma Dec 05 '17 at 18:08
  • @LalitVerma pass-by-pointer is not at all like pass-by-value, unless you're arguing that the value being passed is a ptr. With pass-by-ptr, which BTW used to sometimes be called pass-by-reference before C++ refs came into existence, you don't pass the value, you pass a ptr to the value. With that pointer you can read, and (if not const), even write to the original value instead of a copy of it. You can do the same with C++ pass-by-reference, because "under the hood", C++ references are actually just implicitly-dereferenced const pointers in disguise (not to be confused with pointers-to-const). – phonetagger Dec 05 '17 at 19:27
  • @phonetagger you say "**sometimes be called pass-by-reference before C++ refs came into existence**"", so sometimes called isn't mean it is actually. Search for it and you will find your answer :) – Lalit Verma Dec 06 '17 at 05:12

2 Answers2

2

Because the change you made when you passed the value is on the copy of the variable passed. This is completely unrelated with the variable in main().

On the second case when you passed the address as an value, you dereferenced it and made changes to that address's variable. That's why the change retained.

There is no pass by reference in C.

user2736738
  • 30,591
  • 5
  • 42
  • 56
  • I think simply saying "there is no pass by reference in C" is misleading. In modern times the term "reference" has taken on a stricter meaning that it traditionally held. Only "a few short years ago" in CS and engineering classes teaching C, we were taught to "pass by reference" (meaning by pointers) for two purposes: efficiency and mutability of the original data. Perhaps the more correct statement would be that "C does not pass by reference unless you explicitly make it do so." See also: https://stackoverflow.com/a/37136833/1245420 – phonetagger Dec 05 '17 at 19:58
  • @phonetagger.: *Emulating* pass by reference and *having* pass by reference is different thing. Thequestion you shared, also mentions that yes "There is no pass by reference in C*(last line). I am going a bit more further by saying the emulation that we opt for in `C` to achieve the *pass by reference* is completely different from how C++ implements it. Passing an address is no magic. The address is passed as value too. You statement that *..explicitly make it do so.." - is not what happens. We ***can't*** make PBR in C - we get close to it by using PBV. That's all there is. – user2736738 Dec 06 '17 at 02:48
  • 1
    @phonetagger.: Please check this answer once https://stackoverflow.com/questions/410593/pass-by-reference-value-in-c Here you will see when we can say that we are passing by reference. The compare it with how we emulate *PBR* in C. – user2736738 Dec 06 '17 at 02:49
0

Pass by value : The copy of variable will be passed, change occurs on that copy not in actual variable
pass by address : The address is passed inside function so the change will occur on variable stored at that address

Lalit Verma
  • 782
  • 10
  • 25