...
/Pointer Arithmetic and Types of Pointers
Pointer Arithmetic and Types of Pointers
Learn the basics of pointer arithmetic and types of pointers.
We'll cover the following...
Pointers are a fundamental concept in programming languages like C and C++, providing a powerful way to interact with memory. They allow us to access and manipulate data indirectly by storing memory addresses. To understand pointer manipulation, we first need to understand the deeper meaning of pointer arithmetic. In this lesson, we’ll explore the concept of pointers, their connection with memory, and their application in pointing toward the memory of different types, like readable and writable memories. To explain this connection, we’ll explore the four types of basic pointers, each with its own characteristics and examples. So, let’s dive in!
Pointer arithmetic
Pointer arithmetic refers to the mathematical operations performed on pointers, which allows us to navigate through memory efficiently. Pointer arithmetic is an essential aspect of working with arrays, data structures, and dynamic memory allocation. Here’s an in-depth explanation of pointer arithmetic.
// Translates top++; // p = p + sizeof(Type);p--; // p = p - sizeof(Type);p+=2; // p = p + n*sizeof(Type);int numbers[] = {1,2,3,4};int n = &numbers[3] - numbers[1]; // translates to (&(numbers[3] - &(numbers[1]))) / sizeof(Type)
Incrementing/decrementing a pointer
When we increment a pointer, such as p++, the pointer moves to the next memory location based on the size of the data type it points to. For example, if a pointer p points to an integer, incrementing p by one will move it to the next integer-sized memory location. Similar to incrementing, we can also decrement a pointer, such as p--, which moves the pointer to the previous memory location. The pointer is adjusted by subtracting the size of the data type it points to.
Here’s an example that demonstrates pointer increment/decrement:
Note: The placement of the asterisk (
*) in pointer declarations is a matter of coding style and doesn’t affect the functionality or meaning.int* ptr,int * ptr, andint *ptrare all syntactically correct and equivalent in terms of defining a pointer to an integer.
Pointer arithmetic with integral value
Besides incrementing or decrementing pointers by one, we can perform arithmetic operations with integral values. Adding an integer value to a pointer adjusts its position based on the size of the data type it points to. The general formula for arithmetic with an integral value n is shown below.
p = p+n;// Translates to// ====> p = p + (n * sizeof(Type));
Here’s an example that demonstrates pointer arithmetic with an integral value:
Pointer subtraction
We can also subtract two pointers of the same type to determine the number of elements between them. The result of the subtraction is divided by the size of the data type.
The types of pointers
Understanding the four basic types of pointers and their implications is crucial for writing robust and maintainable code. Each type represents a specific level of mutability and read/write access to memory. In this lesson, we’ll learn about the four types of pointers and provide examples related to array-based manipulation, where the pointers point to various locations within an array. Here, we’re calling it a mutable pointer; it is a pointer whose value (which is an address) can be changed just like any other variable of any primitive data type.
Let’s explore these types in detail.
A mutable pointer pointing to writable memory
A mutable pointer (Type* p) can hold the address of writable memory, enabling both reading and writing operations on the memory it points to. Additionally, the value of the pointer itself can be changed, just like any other variable. This flexibility makes mutable pointers versatile tools for working with and manipulating data in C++.
Here’s an example related to array manipulation:
A mutable pointer pointing to read-only memory (constant memory)
A mutable pointer pointing to read-only memory (const Type* p) allows reading operations but prohibits writing or modifying the data it points to. Here’s an example related to array manipulation:
Immutable pointers pointing to writable memory (const pointers)
Immutable pointers (Type* const p) are constant pointers that hold the addresses of the writable memory. Once assigned, they can’t be reassigned to point to a different memory location. However, they allow modification of the data in memory. Here’s an example related to array manipulation:
The above code shows that modifying the memory through the pointer p is allowed, but changing the pointer address itself is prohibited.
Tip: Lines 13 and 14 will generate a syntax error because modifying the pointer is prohibited since the pointer itself is immutable. Comment it and then execute the program.
Exercise: Find the error in the code
What do you think is wrong with the following code?
Try to correct the above code. If you have trouble getting to the solution, you can click the “Show Hint” button to see how to solve the problem.
Remember: The static arrays are similar to pointers because both of them are constant. That is, if we try to increment the name of the array, it generates a similar error saying that the array name can’t be used as an
. lvalue An lvalue is an expression or object that can be assigned a value and appears on the left side of an assignment operator.
Tip: The error is on Line 9. Comment the line and execute the program again.
Immutable pointers pointing to read-only memory (const pointers and const memory)
Immutable pointers pointing to read-only memory (const Type* const p) provide the highest level of immutability. They can’t be reassigned to point to a different memory location and the data they point to can’t be modified. Here’s an example related to array manipulation:
Tip: Lines 12 and 13 are errors because the pointer itself is immutable, and the memory it points to is read-only. However, line 15 will execute without errors because it does not involve changing the pointer or modifying the read-only memory. Instead, it creates temporary variables (one for a temporary address and another for a temporary value) and manipulates those temporary variables, which is allowed.
Quiz
Let’s check your understanding of pointer types and their accessibility.
Study the following pointer:
Type* p = some_memory_address;
(Select all that apply.) Which of the following manipulations through p are allowed? Multi-select
p++;
p--;
*p = some_value;
*(p+1) = value;
*(++p) = some_value;
Remember: Writable memory like
Type A[] = {some values};can be pointed to by a pointer that requires reading or writing access, such as all four types of pointers.
But the read-only memory like
const Type A[] = {some values};can only be pointed to by the pointer that requires read-only access, which only includes
const Type*orconst Type* constpointers.