Pointer arithmetic
The C++ language allows you to perform integer addition or subtraction operations on pointers. If ptr
points to an integer, ptr + 1
is the address of the next integer in memory after ptr. ptr - 1
is the address of the previous integer before ptr
.
Note that ptr + 1
does not return the memory address after ptr
, but the memory address of the next object of the type that ptr
points to. If ptr
points to an integer (assuming 4 bytes), ptr + 3
means 3 integers (12 bytes) after ptr
. If ptr
points to a char
, which is always 1 byte, ptr + 3
means 3 chars (3 bytes) after ptr.
When calculating the result of a pointer arithmetic expression, the compiler always multiplies the integer operand by the size of the object being pointed to. This is called scaling.
Consider the following program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> int main() { int value{ 7 }; int *ptr{ &value }; std::cout << ptr << '\n'; std::cout << ptr+1 << '\n'; std::cout << ptr+2 << '\n'; std::cout << ptr+3 << '\n'; return 0; } |
On the author’s machine, this output:
0012FF7C 0012FF80 0012FF84 0012FF88
As you can see, each of these addresses differs by 4 (7C + 4 = 80 in hexadecimal). This is because an integer is 4 bytes on the author’s machine.
The same program using short
instead of int
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> int main() { short value{ 7 }; short *ptr{ &value }; std::cout << ptr << '\n'; std::cout << ptr+1 << '\n'; std::cout << ptr+2 << '\n'; std::cout << ptr+3 << '\n'; return 0; } |
On the author’s machine, this output:
0012FF7C 0012FF7E 0012FF80 0012FF82
Because a short is 2 bytes, each address differs by 2.
Arrays are laid out sequentially in memory
By using the address-of operator (&), we can determine that arrays are laid out sequentially in memory. That is, elements 0, 1, 2, … are all adjacent to each other, in order.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> int main() { int array[]{ 9, 7, 5, 3, 1 }; std::cout << "Element 0 is at address: " << &array[0] << '\n'; std::cout << "Element 1 is at address: " << &array[1] << '\n'; std::cout << "Element 2 is at address: " << &array[2] << '\n'; std::cout << "Element 3 is at address: " << &array[3] << '\n'; return 0; } |
On the author’s machine, this printed:
Element 0 is at address: 0041FE9C Element 1 is at address: 0041FEA0 Element 2 is at address: 0041FEA4 Element 3 is at address: 0041FEA8
Note that each of these memory addresses is 4 bytes apart, which is the size of an integer on the author’s machine.
Pointer arithmetic, arrays, and the magic behind indexing
In the section above, you learned that arrays are laid out in memory sequentially.
In the previous lesson, you learned that a fixed array can decay into a pointer that points to the first element (element 0) of the array.
Also in a section above, you learned that adding 1 to a pointer returns the memory address of the next object of that type in memory.
Therefore, we might conclude that adding 1 to an array should point to the second element (element 1) of the array. We can verify experimentally that this is true:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
#include <iostream> int main() { int array []{ 9, 7, 5, 3, 1 }; std::cout << &array[1] << '\n'; // print memory address of array element 1 std::cout << array+1 << '\n'; // print memory address of array pointer + 1 std::cout << array[1] << '\n'; // prints 7 std::cout << *(array+1) << '\n'; // prints 7 (note the parenthesis required here) return 0; } |
Note that when performing indirection through the result of pointer arithmetic, parenthesis are necessary to ensure the operator precedence is correct, since operator * has higher precedence than operator +.
On the author’s machine, this printed:
0017FB80 0017FB80 7 7
It turns out that when the compiler sees the subscript operator ([]), it actually translates that into a pointer addition and indirection! Generalizing, array[n]
is the same as *(array + n)
, where n is an integer. The subscript operator [] is there both to look nice and for ease of use (so you don’t have to remember the parenthesis).
Using a pointer to iterate through an array
We can use a pointer and pointer arithmetic to loop through an array. Although not commonly done this way (using subscripts is generally easier to read and less error prone), the following example goes to show it is possible:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
#include <iostream> #include <iterator> // for std::size bool isVowel(char ch) { switch (ch) { case 'A': case 'a': case 'E': case 'e': case 'I': case 'i': case 'O': case 'o': case 'U': case 'u': return true; default: return false; } } int main() { char name[]{ "Mollie" }; int arrayLength{ static_cast<int>(std::size(name)) }; int numVowels{ 0 }; for (char *ptr{ name }; ptr < (name + arrayLength); ++ptr) { if (isVowel(*ptr)) { ++numVowels; } } std::cout << name << " has " << numVowels << " vowels.\n"; return 0; } |
How does it work? This program uses a pointer to step through each of the elements in an array. Remember that arrays decay to pointers to the first element of the array. So by assigning ptr
to name, ptr
will also point to the first element of the array. Indirection through ptr
is performed for each element when we call isVowel(*ptr)
, and if the element is a vowel, numVowels
is incremented. Then the for loop uses the ++ operator to advance the pointer to the next character in the array. The for loop terminates when all characters have been examined.
The above program produces the result:
Mollie has 3 vowels
Because counting elements is common, the algorithms library offers std::count_if
, which counts elements that fulfill a condition. We can replace the for
-loop with a call to std::count_if
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#include <algorithm> #include <iostream> #include <iterator> // for std::begin and std::end bool isVowel(char ch) { switch (ch) { case 'A': case 'a': case 'E': case 'e': case 'I': case 'i': case 'O': case 'o': case 'U': case 'u': return true; default: return false; } } int main() { char name[]{ "Mollie" }; auto numVowels{ std::count_if(std::begin(name), std::end(name), isVowel) }; std::cout << name << " has " << numVowels << " vowels.\n"; return 0; } |
std::begin
returns an iterator (pointer) to the first element, while std::end
returns an iterator to the element that would be one after the last. The iterator returned by std::end
is only used as a marker, accessing it causes undefined behavior, because it doesn’t point to a real element.
std::begin
and std::end
only work on arrays with a known size. If the array decayed to a pointer, we can calculate begin and end manually.
1 2 3 4 5 |
// nameLength is the number of elements in the array. std::count_if(name, name + nameLength, isVowel) // Don't do this. Accessing invalid indexes causes undefined behavior. // std::count_if(name, &name[nameLength], isVowel) |
Note that we’re calculating name + nameLength
, not name + nameLength - 1
, because we don’t want the last element, but the pseudo-element one past the last.
Calculating begin and end of an array like this works for all algorithms that need a begin and end argument.
Quiz time
Question #1
Why does the following code work?
1 2 3 4 5 6 7 8 9 10 |
#include <iostream> int main() { int arr[]{ 1, 2, 3 }; std::cout << 2[arr] << '\n'; return 0; } |
Question #2
Write a function named
find
that takes a pointer to the beginning and a pointer to the end (1 element past the last) of an array, as well as a value. The function should search for the given value and return a pointer to the first element with that value, or the end pointer if no element was found. The following program should run:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
#include <iostream> #include <iterator> // ... int main() { int arr[]{ 2, 5, 4, 10, 8, 20, 16, 40 }; // Search for the first element with value 20. int *found{ find(std::begin(arr), std::end(arr), 20) }; // If an element with value 20 was found, print it. if (found != std::end(arr)) { std::cout << *found << '\n'; } return 0; } |
Tip
std::begin
and std::end
return an int*
. The call to find
is equivalent to
1 |
int *found{ find(arr, arr + std::size(arr), 20) }; |
![]() |
![]() |
![]() |
Hello, i can't understand one thing of the example in the section "Using a pointer to iterate through an array"
In the next lines of code, i got it how the *ptr variable works, but not how the condition is doing it.
Why there's no problem using two different variables in the for loop condition?
The loop condition is identical to
The site is going through some issue I guess. If I upload a question, it doesn't show the uploaded question after I reload the page. (yeah I have checked and rechecked using different browsers) I have posted two questions previously from "thedeathangeldrax....." email and none of the questions appears in the site sadly. Please fix it if there is a problem.
Consider the code below. I am having some confusions.
Output:
0xbed9c574
0xbed9c578
0xbed9c574
0xbed9c57c
Question: What's the difference between "arr" and "&arr"? Why do "arr+1" and "&arr+1" print different memory addresses?
Explain me what's going on in the code below. I am having some confusions.
Output:
0xbef95574
0xbef95578
0xbef95574
0xbef9557c
Question: What's the difference between "arr" and "&arr"? Why do "arr+1" and "&arr+1" print different memory address.
Here's my answer to Question #2:
It works, but is it good?
Could you explain your thought process when you wrote that function? Here's mine:
1. I need to search for something, so I'll use a for loop
2. I need to loop through all memory addresses between `begin` and `end`
2.1 I'll use an integer offset as the loop variable
2.2 The last address that needs to be ckecked is `end`, so I'll use `i <= (end - begin)` as the condition
3. I need to test whether the value of the current address is equal to `value`
3.1 The current address is `begin + i`
3.2 The value of the current address is therefore `*(begin + i)`
3.3 That needs to be equal to value, so the condition is `if (*(begin + i) == value)`
4. If nothing is found, the loop will exit, and since the end pointer needs to be returned in this case, I'll `return end` after the loop.
Your solution seems more elegant than mine, and while I understand how it works, I don't understand how you got to it.
Your thought process was very good imo, I did pretty much the same thinking, but my solution was the same as the one they've provided. I think at 2.1 you did a bit extra work since you wanted to use an integer offset but incrementing a pointer (++ptr) is the same as *(ptr+1) so you didn't really need to have an integer as your loop control variable, instead copy the begin to another pointer variable and use it to control the loop, and then return it if the condition is met.
I agree with Sahil, your code was very good. But both you and the original lesson example have unnecessary extra work, in your case you wouldn't need the integer 'i' to iterate through the loop, and the original lesson didn't need to assign the pointer 'begin' to another pointer called 'p', if they used the own 'begin' pointer to iterate it wouldn't be any problem, as changing it's value only changes to where it is point at, the value it's point at wouldn't change.
There is a reference to the lesson "6.8 -- Pointers and arrays" in the second section of this lesson. However, the lesson number is different now (9.10). Consequently, in lesson 9.10 you refer to lesson 6.1 -- Arrays (I) in the very beginning. Instead, it should be 9.1. I know, referencing can be a pain (upvote for LaTeX).
I read that each byte of memory is given an address. So for a 4 byte object, will it take up 4 addresses of memory? (with its actual address being the first address in the group of 4)
Yes
Hi Nascar and Alex,
Your solution for second condition is spot on :D
So close on Question 2! Just had some scoping and syntax issues, but my initial answer was really close to the solution on first try.
I added some functionality to receive the number as an user input and recycled some code from 9.3 to find the index of the number too! :)
This is my answer for question #2; is it ok to directly use the pointer to the beginning of the array that was passed in as an argument? I see that the official solution didn't do that. Also, is it ok to use operator< instead of "!="?
> is it ok to directly use the pointer to the beginning
Yes
> is it ok to use operator< instead of "!="? For pointers yes, but this won't work for iterators (later) Name variables descriptively, "b" and "e" are barely helpful.
Alright, thank you!
1. Feedback. Quiz 2 was hard to start. This was my initial thoughts. It's junk. The main difficulty lay in what do I initialize the paramaters as, *ptr or ptr.
Hi. I'm sorry if the code looks messy, I'm still working on it but there's an error on line
that prevents me from advancing further. It says 'cannot convert char** to char* in intialization'.
Can you help me check?
`theString` is a `char[]`, which is the same as a `char*`. The `&` operator returns a pointer to the variable. The variable is a `char*`, so a pointer to a `char*` is a `char**`.
How can I get the value of a 2D array using adding and indirection, for example: int arr[3][2] = {{7,2}, {3,4}, {5,6}};
I have tried things like *(arr + 3) to get the value of position arr[1][0] but it doesn't work. It returns me a memory address.
Sorry for the spelling, I don't speak English. Thanks!
`*(arr + 3)` would return the subarray at index 3 (Which doesn't exist). Let's stick with `arr[1][0]`, because that's a valid element.
First you need to find the subarray at index 1, that's `*(arr + 1)`.
Then you get get element 0 from this subarray using another indirection `*(*(arr + 1) + 0)`.
Just a brief observation: you've begun to use the term 'dereference' in this lesson, but in a way that suggests we should know what it means. I don't *think* you've defined it for us previously, so something of an intro to the implications of the term might be useful here. Thanks!
"Dereference" was briefly mentioned in lesson 6.7, it's a synonym for "indirection through". I must have forgotten to update this lesson, thanks for pointing out the inconsistency!
this program with ptr++ points to next memory address not next integer
and this show me:
0 : 0000001267EFF800
1 : 0000001267EFF804
2 : 0000001267EFF808
3 : 0000001267EFF80C
4 : 0000001267EFF810
5 : 0000001267EFF814
6 : 0000001267EFF818
7 : 0000001267EFF81C
8 : 0000001267EFF820
9 : 0000001267EFF824
These blocks are in a row but you said ptr++ points to next integer
please explain me thanks
0000001267EFF800
the next memory address is
0000001267EFF801
but the next integer is at address
0000001267EFF804
On my machine, if I run this code
I get the following output:
the memory add of the first element is: 00F6FD98
the memory add of the second element is: 00F6FD9C
the memory add of the third element is: 00F6FDA0
5 at the memory address: 00F6FD8C
Why the two memory addresses for the second element are different?
is the address of the `found` variable, not of the array element.
`found` is a pointer to the found element. `&found` is a pointer to the pointer to the found element.