This isn't a question but just something I find useful and hopefully others might also find useful, to get my head around the different uses of the *
operator in pointer syntax.
While it seems like the expression *p
can take on a few different meanings depending on context, it can pretty consistently be read as "the value of p". I'll illustrate with some examples (compared against regular/non-pointer equivalents):
int x; // define x to be an int
int *p; // define the value of p to be an int
x = 5; // set x to 5
*p = 5; // set the value of p to 5
int y;
y = x; // set y to x
y = *p; // set y to the value of p
It even works for functions, kinda:
int foo() // the thing foo returns will be an int
{
...
}
int *foo() // the value of the thing foo returns will be an int
{
...
}
It's not perfect obviously, but it's maybe a useful fallback and preferable to trying to remember how each of the contexts work independently.