I think you're creating some unnecessary confusion by using the same
name "ptr" for two different things. Yes, they're both pointers, but
they're different kinds of pointers; one is an object (a variable) of
type char*, and the other is a parameter of type char**.
In real code, you presumably wouldn't use the name "ptr" for either of
them (nor would you call a function "func"). Entities should
generally have names that describe what you use them for, not how
they're declared (as a pointer or a function or whatever).
So let's try something *slightly* more realistic to demonstrate the
point:
#include <stdio.h>
void set_string(char **target)
{
*target = "Hello, world";
/*
* Of course this assignment doesn't copy the string;
* it assigns the address of the string literal to
* *target.
*/
}
int main(void)
{
char *message;
/*
* message is a char*, not yet initialized
* (it doesn't yet point to anything)
*/
/*
* Now we pass the address of message to the
* set_string function, allowing set_string
* to initialize message for us.
*/
set_string(&message);
/*
* Let's see if it worked.
*/
puts(message);
return 0;
}
--
Keith Thompson (The_Other_Keith) <
[email protected]>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"