I have a question regarding sending a pointer from function1
to function2
whereas function2
changes that pointer. lets take a look at a 'homemade' strcpy
:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void strcopy(char* targ, char* src) {
while(*src) {
*targ = *src;
targ++;
src++;
}
*targ = 0;
}
int main() {
char* src = "this is aa string";
char* targ = malloc(strlen(src)*sizeof(*targ)+1);
strcopy(targ, src);
printf("'%s'\n", targ);
return 0;
}
the output is 'this is aa string'
the Question: how comes targ
is pointing at the beginning of the string although we did targ++
in the function? it seems like we're sending the pointer by value!! could anyone give a interpretation for that? Thanks.
When you pass a pointer into a function, you're indeed passing the value of the pointer, your "targ" variable is a copy of the targ pointer in your main() function. Incrementing it inside strcopy() has no effect on your original pointer. When you pass a variable by value, you're indeed passing an hidden pointer to the original variable, but in this case, you're storing the memory address into a new variable on the stack.
void strcopy(char* targ, char* src)
When you call this API targ
will get value of the pointer targ
in main()
so targ
in main is never moved just the API is filling the values to the location rename the argument to something else for clarity.
void strcopy(char* dest, char* src)
So now you can read it as the pointer dest is moved to copy the values and targ
in main()
remains intact
it seems like we're sending the pointer by value
Indeed you are!
In C, everything is passed by value. Not until C++ did we have the notion of passing something by reference. In this case, you're passing a copy of the value of the pointer (ie the memory address that it is pointing to).
If you wanted to change what targ
was pointing to, you could write this:
void strcopy(char** targ, char* src) { /* ... */ }
strcopy( &targ, src );
The targ
in your function and the targ
in main
are very different from each other. In fact, it might help your understanding if we changed the name of the parameter inside strcopy
:
void strcopy(char* first_pointer, char* second_pointer) {
while(*second_pointer) {
*first_pointer = *second_pointer;
first_pointer++;
second_pointer++;
}
*first_pointer = 0;
}
I admit that is less readable, but it is actually identical to your version. Inside a function, the names of the parameters are really just 'nicknames' for 'first argument', 'second argument', and so on.
When you pass a value into a function, the 'name' of that value is meaningless until the function returns.
In C, everything is passed by value, by default. If you call foo(x,y)
, then x
and y
will not change. If you put &
in front, then it is possible to pass x
'by reference', allowing foo
to modify x
in the caller, but that is written foo(&x,&y)
. If you don't see &
in front of the value, then it is by value.
And passing a pointer by value means that the original pointer will continue to point to the same location. Of course, the data pointed to by the pointer might change sometime, but the pointer itself will point to the same location.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.