Page Content

Tutorials

C++ Pointers: Addresses, Dereferencing, And More

C++ Pointers

Pointers are a fundamental concept in C++, which enable direct memory access and provide a great deal of flexibility and speed enhancement possibilities, may also be a major source of problems if not handled properly. A memory address, or the location of another item in memory, is stored in a pointer, which is a variable. This enables access to other objects in an indirect manner. In contrast to references, pointers are objects in and of themselves, which means they can be copied, assigned, and used to point to many objects during the course of their lifespan.

Pointers are explained here, along with how to declare them and use the address-of (&) and dereferencing (*) operators:

Declaring Pointers

C++ pointers are declared with asterisks (*). A variable declared with * is a pointer to a type. Pointer arithmetic is always relative to its base type, therefore its base type dictates what it can point to.

A pointer variable is generally declared as follows: type *var_name;

Example: int *ip; to declare a reference to an integer

Key Declaration Conventions:

  • The * is typically placed next to the variable name (for example, int *ptr;). By highlighting the fact that the * only applies to the immediate variable, this helps prevent deceptive declarations when declaring several variables in one statement.
  • You have to repeat the * for every pointer variable if you declare more than one variable in a single statement:
  • Multiple indication, or pointers to pointers, is the ability to have one pointer that points to another. A number of asterisks are used to denote this.
  • It requires many dereference operator applications to obtain the value via a pointer to a pointer: cout << **ppi << endl; // Prints 102432

address-of operator (&)

Unary address-of returns operand memory address. After that, a pointer variable normally gets this address.

Example:

#include <iostream>
int main() {
    int var = 42;       // Declare an integer variable 'var' 
    int *ptr;           // Declare a pointer to an int named 'ptr' 
    ptr = &var;         // Assign the address of 'var' to 'ptr' using the & operator 
    std::cout << "Value of var: " << var << std::endl;         // Output: Value of var: 42
    std::cout << "Address of var: " << &var << std::endl;       // Output: Address of var: 0x... (memory address) 
    std::cout << "Value stored in ptr (address of var): " << ptr << std::endl; // Output: Value stored in ptr: 0x... (same memory address) 
    return 0;
}

Output

Value of var: 42
Address of var: 0x7fffca39e0ec
Value stored in ptr (address of var): 0x7fffca39e0ec

Important Note: In C++, the & symbol can be used as an expression’s address-of operator or to define a reference. Context helps the compiler understand. P = &i; uses the address-of operator &, while int &r = i; declares a reference r.

dereferencing operator (*)

Dereferencing or indirection operator (*) retrieves a variable’s value from a pointer. Dereferencing pointers returns their objects.

Use the dereferenced pointer to set the object’s value as well as read it.

Example:

#include <iostream>
int main() {
    int ival = 42;       // Declare and initialize an integer variable 'ival' 
    int *p = &ival;      // Declare a pointer 'p' and initialize it with the address of 'ival' 
    std::cout << "Value of ival: " << ival << std::endl;           // Output: Value of ival: 42
    std::cout << "Value pointed to by p (*p): " << *p << std::endl; // Output: Value pointed to by p (*p): 42
    *p = 99;             // Change the value of the object 'p' points to (which is 'ival') to 99 [58-61, 69, 70]
    std::cout << "New value of ival after dereferencing assignment: " << ival << std::endl; // Output: New value of ival: 99
    std::cout << "New value pointed to by p (*p): " << *p << std::endl; // Output: New value pointed to by p (*p): 99
    return 0;
}

Output

Value of ival: 42
Value pointed to by p (*p): 42
New value of ival after dereferencing assignment: 99
New value pointed to by p (*p): 99

Important Note: The * symbol has two meanings, just like the & operator. It is employed in both dereferencing (*ptr = value;) and pointer declaration (int *ptr;). What it does depends on the circumstance.

Pointers and their “Null” State

Null pointers point to nothing. To avoid unsafe memory addresses, initialize unused or unbound pointers to nullptr (or NULL or 0 in older C++ versions). Crashes and hard-to-trace errors result from uninitialized pointers writing to unknown memory locations.

Pointers vs. References

There are some significant distinctions between pointers and references, even though both offer indirect access to objects:

  • Object Status: Being an object in and of itself, a pointer has the ability to be copied, assigned, and its pointer object can change over time. References are not objects in and of themselves; they are only aliases for already-existing objects.
  • Initialization: A reference must be initialized at declaration to refer to a separate object. Pointers, like other built-in types, can point to other objects after declaration and are not necessary to be initialized, but this is recommended.
  • Null State: Null pointers are those that point to nothing. It is forbidden to use null references.
  • Address of a Reference: References cannot be pointed to or arranged since they are not objects.

Pointer usage is essential for arrays, dynamic memory allocation, and sophisticated data structures in C++.

You can also read C++ Arrays: With Practical Code Examples And Applications

Agarapu Geetha
Agarapu Geetha
My name is Agarapu Geetha, a B.Com graduate with a strong passion for technology and innovation. I work as a content writer at Govindhtech, where I dedicate myself to exploring and publishing the latest updates in the world of tech.
Index