Hi there guys, I've been reading about pointers lately as they're one of the most important things to become comfortable with. I think I have the main idea down, but I still have a bit of confusion, so I'd like to ask a couple of questions.
#1 "pCake = &dog" - pCake's value is now dog's address, right? Therefore pCake is now a pointer to dog.
dog = 25;
pCake = dog;
pCake = &dog;
#2 "pCake = dog" - Is this the same as #1? Does this set pCake's value to dog's address just as it does in #1? Or does this infact set pCake's value to 25?
If this is the case, derefercing pCake (if pCake = dog not &dog) would point to an address of "25" and print the variable/value that is being held at the address "25"? But since "25" isn't a valid address, it wouldn't work, therefore what would be the point of using "pCake = dog" instead of "pCake = &dog"? Do they do the same thing, does the compiler automatically put the "&" there to specify address?
I've been thinking about this and, maybe if the code was like this it would be useful, but I can't see a use for this:
Now pCake's value should = dog's value? Am I correct? Therefore pcake is equivalent to 0x0000204, which is basically the same as a pointer, but instead of it pointing to dog, it's infact pointing to another variable (which I haven't set in this example).
int dog = 0x0000204;
/* random address in this examplebut maybe it actually points to
something in reality, i.e a variable used elsewhere. */
int pCake = dog;
I've tried to explain what I'm talking about best I could but it's pretty confusing so yeah. Basically I want to know if pCake = &dog; and pCake = dog; are the same (maybe the compiler automatically assumes dog's address - not it's value, because it's dealing with a pointer - or maybe not, please let me know), also if they aren't the same, then what use would leaving out the & have.. Thanks! hehe