Basic Pointers and Dereferencing in C++

Basic Pointers and Dereferencing in C++

Pointers in the C++ programming language are user-defined, specific memory variables that hold the memory addresses of other variables in a program. Pointers allow C++ users to modify program memory directly, access C++ program objects, and implement complex data structures such as linked lists and trees.

Basic Pointers and Dereferencing in C++

So, let’s get to know the pointer data type in C++ programming better.

  • Declaring a pointer parameter in C++ programming.
  • Initializing a pointer variable data type.
  • Dereferencing a pointer variable data type.
  • Using a pointer data type with arrays and dynamic memory variables.

So, let’s understand the pointer data type concept in C++ programming in detail.

Declaring and initializing pointer data types in C++ programs.

Declaring a Pointer in C++ Programming A pointer variable is declared using the * symbol multiplication operator. Here, the pointer’s data type properly matches the data type of the variable it points to.

Pointer Data Type Declaration Syntax.

Type* pointerVariableName;

Element of Pointer Data Type.

  • Here, the pointer’s type is the data type of the variable the pointer variable will point to. For example, int, char, double, etc., the data type is the method.
  • Here, the user-defined pointerVariableName is the name of the pointer variable in the current program.

Example of a Pointer Data Type variable.

int* ptr; // Here, a pointer to an integer variable is declared.

Here, ptr is a pointer variable declaration for an int data type. It can store a memory address value of an integer data type.

Initializing Pointer Variables.

In a C++ program, a pointer is initialized by assigning it the memory address of a variable. C++ users can use the address-of operator (&) symbol to display the memory address of a declared pointer variable in a program.

int test = 19; // Here we have declared a simple integer variable

int* ptr = &test; // Here test is initialized to the address of the pointer variable

Here, in this example, the ptr pointer now points to the test variable.

Dereferencing Pointer Data Variables.

In a C++ program, dereferencing a pointer means accessing the value stored at the memory address of the pointer variable. C++ users can do this by applying the dereference operator (*).

Syntax of Dereferencing Pointer.

*pointerVariableName

This provides C++ users with the value stored at the memory address stored in the pointer in the current program.

Example of Dereferencing Pointer.

#include <iostream>

int main() {

int test = 19; // Here we declare a regular integer variable

int* ptr = &test; // Here the pointer is initialized to the address of the test variable

std::cout << “The value of the test variable is – ” << test << std::endl; // Here it is directly accessing test

std::cout << “Here the pointer points to its value – ” << *ptr << std::endl; // Here it is dereferencing ptr

return 0;

}

Explanation of Dereferencing Pointer.

  • In this example, *ptr dereferences the pointer ptr, giving the C++ user the value stored at the memory address it points to. In this case, it is 19.

Pointer Arithmetic in C++.

In addition to pointer dereferencing, C++ programs also fully support pointer arithmetic operations. Pointer arithmetic helps C++ users traverse arrays or memory blocks in a proper sequence.

Syntax of Pointer Arithmetic.

  • Increment a pointer – In a C++ program, ptr++ moves the pointer data type ptr to the next memory location it points to.
  • Decrement a pointer – In a C++ program, ptr moves to the previous memory location.
  • Add an integer to a pointer – In a C++ program, ptr + n moves forward by n positions.
  • Subtract an integer from a pointer – ptr in C++ program – moves n positions backward.

Example of Pointer Arithmetic.

#include <iostream>

int main() {

int array[] = {98, 44, 59, 67, 55, 99};

int* ptr = array; // here we define pointer to the first element of the array

std::cout << “First array element is – ” << *ptr << std::endl; // here we are dereferencing ptr (points to array[0])

ptr++; // here it moves to the next element in the array

std::cout << “Second array element is – ” << *ptr << std::endl; // here we are dereferencing new position (array[1])

ptr += 2; // here it two positions ahead (points to array[3])

std::cout << “Fifth array element is – ” << *ptr << std::endl; // here we are dereferencing new position (array[3])

return 0;

}

Explanation of Pointer Arithmetic.

  • In this example, ptr++ moves the pointer from arr[0] to arr[1].
  • ptr += 2 moves it from arr[1] to arr[3].
  • Pointer arithmetic in C++ programming is commonly used with arrays, where the pointer variable acts as an iterator, moving through the array’s memory locations.

Null Pointers Concept in C++.

In a C++ program, a null pointer is a user-defined pointer that does not point to a valid memory location. A null pointer is typically used to indicate that the pointer data type has not yet been initialized, or has been explicitly set to not point to an object.

C++ users can initialize pointer data as nullptr (in C++11 and later versions) or NULL (in older versions of C++).

Example of the Null Pointers concept.

#include <iostream>

int main() {

int* ptr = nullptr; // Here the pointer variable is defined as a null pointer (for only C++11 and beyond)

if (ptr == nullptr) {

std::cout << “Pointer value defined as null.\n”;

}

return 0;

}

Explanation of the Null Pointers concept.

  • In this example, ptr is defined to nullptr, and C++ users check whether it is also a nullptr value before attempting to dereference it. Dereferencing a null pointer in a C++ program produces undefined behavior. Therefore, before dereferencing a null pointer, it is important to check whether the pointer is defined to have a null value or not.

Pointers with Arrays Concept in C++.

When C++ users use pointer data with the array variable data type, the pointer variable data is actually a reference to the first element of the array. C++ users can access and manage other pointer array elements using pointer arithmetic.

Example of Pointers with Arrays.

#include <iostream>

int main() {

int array[] = {66, 58, 78, 69, 79, 100, 130};

int* ptr = array; // here we define pointer to the first element of the array

// here we access elements of array using pointer arithmetic method

std::cout << “First array element is – ” << *ptr << std::endl; // Result is – 66

std::cout << “Second array element is – ” << *(ptr + 1) << std::endl; // Result is – 58

std::cout << “Third array element is – ” << *(ptr + 2) << std::endl; // Result is – 78

std::cout << “Next array element is – ” << *(ptr + 6) << std::endl; // Result is – 130

return 0;

}

Explanation of Pointers with Arrays.

  • In this example, ptr points to the first element of the array, and *(ptr + 1) accesses and displays the second element, and so on, pointing to and previewing the next array element.

Pointer to Pointer (Multi-Level Pointers) Concept in C++.

C++ programming allows users to define pointer to pointer data type variables. This is especially useful when working with dynamic arrays or multi-dimensional arrays of pointers in existing C++ programs.

Example of pointer to pointer (Multi-Level Pointers).

#include <iostream>

int main() {

int test = 19; // Here we declare a simple test integer data type variable

int* ptr = &test; // Here we declare a pointer to an integer data type variable

int** ptr2 = &ptr; // Here we declare a pointer to pointer (pointer to ptr) data type variable

std::cout << “Value of ordinary test variable is – ” << test << std::endl; // Result is – 19

std::cout << “Dereferencing ptr value is – ” << *ptr << std::endl; // Result is – 19

std::cout << “Dereferencing ptr2 value is – ” << **ptr2 << std::endl; // Result is – 19

return 0;

}

Explanation of pointer to pointer (Multi-Level Pointers).

  • In this example, ptr2 is a pointer data type variable to ptr, which is a pointer to the test variable. By dereferencing ptr2 twice (**ptr2), we can access the value of test.

Leave a Reply