Same Asterisk Different Meanings

suggest change

Premise

The most confusing thing surrounding pointer syntax in C and C++ is that there are actually two different meanings that apply when the pointer symbol, the asterisk (\*), is used with a variable.

Example

Firstly, you use \* to declare a pointer variable.

int i = 5;
/* 'p' is a pointer to an integer, initialized as NULL */
int *p = NULL;
/* '&i' evaluates into address of 'i', which then assigned to 'p' */
p = &i;
/* 'p' is now holding the address of 'i' */

When you’re not declaring (or multiplying), \* is used to dereference a pointer variable:

*p = 123;
/* 'p' was pointing to 'i', so this changes value of 'i' to 123 */

When you want an existing pointer variable to hold address of other variable, you don’t use \*, but do it like this:

p = &another_variable;

A common confusion among C-programming newbies arises when they declare and initialize a pointer variable at the same time.

int *p = &i;

Since int i = 5; and int i; i = 5; give the same result, some of them might thought int *p = &i; and int *p; *p = &i; give the same result too. The fact is, no, int *p; *p = &i; will attempt to deference an uninitialized pointer which will result in UB. Never use \* when you’re not declaring nor dereferencing a pointer.

Conclusion

The asterisk (\*) has two distinct meanings within C in relation to pointers, depending on where it’s used. When used within a variable declaration, the value on the right hand side of the equals side should be a pointer value to an address in memory. When used with an already declared variable, the asterisk will dereference the pointer value, following it to the pointed-to place in memory, and allowing the value stored there to be assigned or retrieved.

Takeaway

It is important to mind your P’s and Q’s, so to speak, when dealing with pointers. Be mindful of when you’re using the asterisk, and what it means when you use it there. Overlooking this tiny detail could result in buggy and/or undefined behavior that you really don’t want to have to deal with.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents