The * symbol in C++ is used in several different contexts, most commonly for declaring pointer variables, dereferencing pointer variables, and specifying repetition in array declarations. Understanding how to use * correctly is an essential skill for any C++ programmer.
Declaring Pointer Variables
The most common use of * in C++ is for declaring pointer variable types. A pointer variable stores the memory address of another variable rather than storing the value directly.
To declare a pointer variable, the * symbol is placed before the variable name in the declaration:
int* pointerToInt; // pointerToInt is a pointer to an int double* pointerToDouble; // pointerToDouble is a pointer to a double MyClass* pointerToMyClass; // pointerToMyClass is a pointer to an object of MyClass
This tells the compiler that these variables will hold memory addresses rather than values of the given types. The * associates the pointer type with the variable name.
Some key facts about C++ pointer declarations:
- The * goes with the variable name, not the type. For example, int *pointerToInt; declares a pointer variable, while int* pointerToInt; does the same thing.
- Multiple * can be used for multiple indirection (pointers to pointers). For example, int** pointerToPointerToInt declares a pointer to a pointer to an int.
- The pointer type must match the type of variable it points to. A pointerToInt can point to an int variable, but not a double.
Correctly placing the * next to the variable name when declaring pointer types is essential in C++.
Dereferencing Pointers
The * is also used to dereference pointers – this gives access to the variable pointed to by a pointer.
To get the value at the memory address stored in a pointer, place * before the pointer’s name:
int x = 5; int* pointerToX = &x; // pointerToX points to x coutThe * dereferences pointerToX to access the int variable it points to. This can be used to get or set the value at a pointer's address.
Key facts about dereferencing pointers:
- Use *pointerToVar to access the pointed-to variable.
- No spacing around the * for dereferencing.
- Can be chained like **pointerToPointer for multiple indirection.
Dereferencing a pointer gives access to the memory address it stores, while declaring a pointer type associates the * with the variable name indicating it will hold a memory address.
Arrays
The * operator can also be used when declaring arrays in C++. Placing * after the array type specifies there will be a certain number of elements in the array.
For example:
int array1[5]; // Array of 5 ints double array2[10]; // Array of 10 doubles int* array3[15]; // Array of 15 pointers to ints MyClass* array4[20]; // Array of 20 pointers to MyClass objects
The * goes after the type but before the array name, indicating repetition of that type in the array.
Key facts about arrays:
- The * goes after the element type, before the array name.
- This specifies the number of elements in the array.
- Works for both regular arrays and arrays of pointers.
The * in array declarations is entirely different from its usage declaring pointer variables and dereferencing pointers.
Using the * Correctly
Because the * symbol has different meanings in different contexts, it's important to understand when and how to use it correctly in C++. Here are some key tips:
- Declaring a pointer variable? Place * next to the variable name.
- Dereferencing a pointer? Place * directly before the pointer's name with no space.
- Declaring an array? Place * after the element type but before the array name.
- Multiple * can be used for multiple indirection with pointers and arrays of pointers.
- The pointer type must match the type it points to.
- Don't confuse pointer declaration * placement with dereferencing or array * placement.
Getting the * placement correct in all cases will avoid compile errors and unintended behavior in your C++ code.
Examples
Here are some examples of using * correctly in different contexts:
Pointer Variable Declaration
double* ptrToDouble; // Correct - * goes with variable name double * ptrToDouble; // Also correct double *ptrToDouble; // Incorrect - spaces around *
Pointer Dereferencing
int x = 5; int* ptrToX = &x; coutArray Declaration
int array1[5]; // Correct int* array2[10]; // Correct int *array3[15]; // Incorrect - * should be after int int array4[20]*; // Incorrect - * should be before array namePointer to Pointer
int x = 5; int* ptrToX = &x; int** ptrToPtrToX = &ptrToX; // Correct - second * for pointer to pointerArray of Pointers
double* ptrArray[10]; // Correct - array of 10 pointer to doubles MyClass* objPtrArray[12]; // Correct - array of 12 pointers to MyClass objectsConclusion
The * symbol has different meanings in C++ depending on the context in which it is used. Placing it correctly when declaring pointer variables, dereferencing pointers, and specifying arrays is essential to writing valid C++ code. Remembering where * goes in each case and not confusing the contexts will help avoid problems. Mastering proper usage of * will unlock the full power and flexibility of pointers and arrays in C++.