Does the plain literal refer to the address and *literal refer to the actual value at the address? So, AFTER:
int i = 0;
int *iPointer = &i;
the following expression will lookup the VALUE AT memory address &i:
*iPointer
and the following will simply yield the memory address &i:
iPointer
I stepped through and verified my hypothesis, but I want to make sure (you never know with these things).
I guess I'm just confused by the * symbol's different purposes in declaration and access.
When declaring a pointer type, place the asterisk next to the type name. Although you generally should not declare multiple variables on a single line, if you do, the asterisk has to be included with each variable.
Here, * is called dereference operator. This defines a pointer; a variable which stores the address of another variable is called a pointer. Pointers are said to point to the variable whose address they store.
Pointers must be declared before they can be used, just like a normal variable. The syntax of declaring a pointer is to place a * in front of the name. A pointer is associated with a type (such as int and double ) too.
Simply remembering to put one asterisk per pointer is enough for most pointer users interested in declaring multiple pointers per statement.
Yes, that's absolutely correct.
Also, note that &
can also declare a reference, depending on context.
In a declaration, *
declares a pointer type.
When applied on a pointer (like *ptr
), it represents the dereference operator and returns the value the pointer points to.
Note that operator *
can be overloaded, so you can also apply it to objects, not only pointers, and have it do whatever you want.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With