Quote:
Originally Posted by HelloWorld So can I conclude that pointer CAN'T be a variable, it's only something that can be used to reference the other variable's memory. And when we change this pointer's value to something else, the variable that is pointed to is also changed because this is passed by reference...? Feel free to add some more summary that I can use to get better understanding of how does the pointer works. Moreover, I need to use * sign whenever I use pointer throughout my program..? If I don't use it, it's just going to be the memory of that pointer, so if I want to make another pointer to this pointer, I should do the last step.. (I DON'T NEED TO USE & sign) lol.. Let me know if I missed any test in this process.. thanx |
You are correct in your assertion that a pointer is used to reference other variables or objects in a program, and that modifying a pointer causes a different variable or object to be referenced.
As for your statement "A pointer can't be a variable" - I assume you mean that The
identifier (name) for the pointer cannot be used to directly manipulate the referenced variable. Which, again, is correct. The de-reference operator ( * ) must be used to access the referenced variable.
However, in general, a pointer occupies space in memory, and has a data type, therefore a pointer certainly can be a variable itself - It is possible to create a pointer-to-pointer
Code:
int i = 5; //type: int
int* p_i = &i; //type: pointer (-to-int)
int** p_p_i = &p_i; //type: pointer (-to-pointer(-to-int) )
Its important to get out of the mindset where you think of a pointer as being some kind of a mutator for a variable (Which is how alot of people seem to start out), and begin to treat a pointer as a whole new type.