I’ve just started to learn C so please be kind.
From what I’ve read so far regarding pointers:
int * test1; //this is a pointer which is basically an address to the process
//memory and usually has the size of 2 bytes (not necessarily, I know)
float test2; //this is an actual value and usually has the size of 4 bytes,
//being of float type
test2 = 3.0; //this assigns 3 to `test2`
Now, what I don’t completely understand:
*test1 = 3; //does this assign 3 at the address
//specified by `pointerValue`?
test1 = 3; //this says that the pointer is basically pointing
//at the 3rd byte in process memory,
//which is somehow useless, since anything could be there
&test1; //this I really don't get,
//is it the pointer to the pointer?
//Meaning, the address at which the pointer address is kept?
//Is it of any use?
Similarly:
*test2; //does this has any sense?
&test2; //is this the address at which the 'test2' value is found?
//If so, it's a pointer, which means that you can have pointers pointing
//both to the heap address space and stack address space.
//I ask because I've always been confused by people who speak about
//pointers only in the heap context.
Great question.
Your first block is correct. A pointer is a variable that holds the address of some data. The type of that pointer tells the code how to interpret the contents of the address being held by that pointer.
The construct:
Is called the deferencing of a pointer. That means, you can access the address that the pointer points to and read and write to it like a normal variable. Note:
The above is just a mnemonic device that I use.
It is rare that you ever assign a numeric value to a pointer… maybe if you’re developing for a specific environment which has some ‘well-known’ memory addresses, but at your level, I wouldn’t worry to much about that.
Using
would ultimately result in an error. You’d be trying to deference something that is not a pointer, so you’re likely to get some kind of system error as who knows where it is pointing.
&test1and&test2are, indeed, pointers totest1andtest2.Pointers to pointers are very useful and a search of pointer to a pointer will lead you to some resources that are way better than I am.