C passes arguments by value, always, meaning that the called function receives a local copy of whatever the caller refers to.
The called function can modify the received value, because it is a local copy, without affecting the original value. For example:
char *test(char *s) {
s++;
return s;
}
t = test("A");
it's legal and shows that the parameter s can be modified without affecting the caller (which passes a literal...).
But the strcpy() of you example does something different: it takes a pointer s, and modifies what s points to. Pointers are powerful, and they can be used to simulate "pass by reference" (a pointer is a reference).
after assignment *s = *t is held, which one is compared with '\0'? *s or *t?
The *s: in C, an assignment returns a value - and the value is the value of the assigned variable after the assignment is done. Writing:
if (i=3) ...
is the same as
i=3;
if (i) ...
If i was a pointer the syntax would be different but the mechanism is the same, the assignment would "use" the pointer and the value of the whole assignment is used as expression to be evaluated in the test.
Answer from linuxfan says Reinstate Monica on Stack OverflowWhy is everything in C call by value?
Is this call by reference or by value in C? - Stack Overflow
call by value and call by reference
Understanding "Call by Reference" and "Call by Value" in Programming Languages - Seeking Clarification for My Assigned Task
Edit3: THANK YOU SO MUCH, I UNDERSTAND IT BETTER NOW (I hope). Please don't delete any of the explanations because I wanna refer to them in the future when I get confused again. THANK YOU EVERYONE!!
I kind of understood call by reference vs call by value after reading this.
My understanding is int i stores the value and is called by value. int *j points to the memory address which stores the value, this is a call by reference.
I tried to understand CodeTinkerer's explanation but I still don't understand why doesn't it swap 3 and 5.
Doesn't it point to the address and should then swap the contents?
Edit: Referring to change(), not swap, I apologize for not mentioning this earlier.
Edit2: I'm reading everything, I just need time to digest it all. Thank you to those who have replied and I'm open to more explanations so thank you in advance too.
C passes arguments by value, always, meaning that the called function receives a local copy of whatever the caller refers to.
The called function can modify the received value, because it is a local copy, without affecting the original value. For example:
char *test(char *s) {
s++;
return s;
}
t = test("A");
it's legal and shows that the parameter s can be modified without affecting the caller (which passes a literal...).
But the strcpy() of you example does something different: it takes a pointer s, and modifies what s points to. Pointers are powerful, and they can be used to simulate "pass by reference" (a pointer is a reference).
after assignment *s = *t is held, which one is compared with '\0'? *s or *t?
The *s: in C, an assignment returns a value - and the value is the value of the assigned variable after the assignment is done. Writing:
if (i=3) ...
is the same as
i=3;
if (i) ...
If i was a pointer the syntax would be different but the mechanism is the same, the assignment would "use" the pointer and the value of the whole assignment is used as expression to be evaluated in the test.
Many people consider that idiom to be something that’s neither call-by-value or call-by-reference. For what it’s worth, Kernighan and Ritchie’s The C Programming Language does call arrays and pointers in C “references,” although it uses only the term call-by-value and not call-by-reference. That usage seems to have gone out of fashion once C++ added a different language feature called references.
A function that accepts a pointer and dereferences it will generally compile to the same machine code as a function in a language with call-by-reference. One difference that is not merely the absence of syntactic sugar: if the pointer argument itself is not const, it is possible to reassign a new value to it and make it reference something else, which “call-by-reference” semantics do not allow. In a language with call-by-reference, you could not write s++ or t++ to make the function arguments reference different objects! (C strings are stored in such a way that you can add an offset to the pointer to obtain a substring, whereas most langauges store the length of the string in the first few bytes of its memory.) However, you can mostly think of a reference T& in C++ as equivalent to passing a T *const that is guaranteed not to be NULL, and that has an invisible asterisk in front of its name.