I'm reading through a great book on C but it hasn't done a great job making pointers easy to understand. What I'm having trouble with is not the concept of pointers (although I'm still not sure yet how/why it will be useful in writing a program) but how the difference between the use of * and & in terms of pointers...
from what i can tell * is only for declaring a variable as a pointer variable right? so...
would both be valid right?Code:int *myVariable float *myOtherVariable //can a float do this?
what i'm confused about is when i refer to *myVariable from this point on do i need to use *myVariable everytime, or can I simply use myVariable (without the * infront of it) later in the program?
ok, the other question is i saw an example in the book...
i'm going to assume myVariable2 is NOT a pointer variable because there is no * infront of it, right? or is it a pointer variable, because its on the same line as *myVariable?Code:int *myVariable, myVariable2
Ok, the 3rd and final question is about the uses of &...
I assume you can put an & before any variable no matter what, and even if it hasn't been declared earlier as a pointer variable? and does an & mean you are taking the variable's address in memory from it instead of what the actual value store in the variable is?
so would this be correct (would it work)...?
... If the code above is valid, then why couldn't I just do this instead? (referring to the printf line) ...Code:int *myPointerVariable; int boxOfBaseballs; int main( void ) { boxOfBaseballs = 24; *myPointerVariable = &boxOfBaseballs //will store boxOfBaseballs memory address not "24" printf( "This address in memory is: %d", *myPointerVariable ); return 0; }
and another point... the pointer address in memory is going to an interger number so why bother with a *variable to store the information in? why wouldn't it make sense to simply say... someAddress = &whatever;Code:printf( " This address in memory is: %d", &boxOfBaseballs );



LinkBack URL
About LinkBacks



