In the below code, a *Pointer is passed to a function.
The address & symbol is omitted.
The functions can still modify the value in the passed pointer.
Does the compiler auto-add the &?
char * input();
void get_command() {
char * command_pointer = input();
char * second = second_arg(command_pointer); //second needs to be before first
char * first = first_arg(command_pointer); //becuase first adds a /0
choose(first, second);
free(command_pointer);
}
char * first_arg(char * arg) {
char * j = strchr(arg, '-');
if(j==NULL) {
return arg;
}
*j = '\0';
return arg;
}
char * second_arg(char * arg) {
char * j = strchr(arg, '-');
if( j==NULL ) {
return NULL;
}
return j;
}
CodePudding user response:
Does the compiler auto-add the &?
No. &
gets the address of a variable, and the address of command_pointer
isn't passed. Only its value (the address of the buffer) is passed.
The functions can still modify the value in the passed pointer.
No, they can't. They can't modify (the value of) the pointer. They can only modify that to which the pointer points.
first_arg
and second_arg
can't change command_pointer
(the variable in get_command
) because they don't know anything about it and they don't have a pointer to it.
e.g. arg = NULL;
would have no effect on command_pointer
.
However, they can modify the data to which command_pointer
points (the char
s), because they have a copy of the same pointer.
e.g. *arg = 0
(aka arg[0] = 0;
) would effect *command_pointer
(aka command_pointer[0]
).
Let's illustrate some of the variables of get_command
and first_arg
.
input()'s rv Buffer allocated by input
---------- --- --- --- --- --- --- ---
| 0x1000 ------- --->| | | | | | | ...
---------- | --- --- --- --- --- --- ---
| ^
command_pointer | |
---------- | |
| 0x1000 ------- |
---------- | |
| |
arg | j |
---------- | ---------- |
| 0x1000 ------- | 0x1004 ---------
---------- ----------
input()
returns an address to a bufer it allocated (0x1000
in the diagram).- The value returned by
input()
is copied intocommand_pointer
. - The value of
command_pointer
is copied intoarg
. - A value based on
arg
is stored inj
. get_command
can modifycommand_pointer
and the buffer.first_arg
can modifyarg
,j
and the buffer, but notcommand_pointer
.
CodePudding user response:
The value is changed only if you use the * on the pointer. if you will not the value will not change, the address will change. Consider these functions:
char * first_arg(char * arg) {
arg = 'a';
// some other code //
}
char * second_arg(char * arg) {
*arg = 'c';
// some other code //
}
the first function will change the address that arg is pointing at. This function can be dangerous because we don't now what is stored in the address 'a'. This function may change the behavior of otter scopes in an unexpected way. but it will not change the value that arg is pointing at. (except of the scenario that you hardcode the exact address of arg)
the second function will change the value in the address arg is pointing at. This is the well defined behavior that we are looking for. This will only change the value of the memory arg is pointing at.