Before you start

Rick
3 min readJan 25, 2023

If you didn’t read the previous post and you have a limited understanding, take a moment to read the post here.

Arrays work with pointers

When an array is declared, enough space is allocated to save the elements. In addition array names are converted to pointers, this means that we can use pointers to access the addresses and values of each pointer through pointers.

try to understand the following example before reading the explanation, what do you think the output is?

#include <iostream>


using namespace std;


int main(int argc, char const *argv[]){


int arr[5] = {1,2,5,4,5};
cout << arr << endl;
cout << *arr << endl;

}

The first cout << arr will output the address of the first element. When we add *arr we are saying that we want the contents of the first element.

>> 0xa3bc7ff8b0
>> 1

Initializing Pointers

Something that is important to do when working with pointer is initializing them, before getting into that, let’s see these 2 examples

int number = 17, *ptr;
ptr = &number;
cout << *ptr << endl;

since the pointer is initialized with the variable number the the output is 17, what happens if we slightly change the code to this:

int number = 13, *ptr;
*ptr = number;
cout << *ptr << endl;

Now, one of two things may have happened. The first is that it printed out 13 like normal. However, If you have a memory related error, it’s most likely because we are not initializing the pointer correctly.

Here are some ways to initialize properly a pointer

int *ptr1 = new int;  // option 1
*ptr1 = 5;

int variable = 5; // option 2
int *ptr2 = &variable;


int *ptr3 = nullptr; // option 3

Iterators in arrays

below there’s an example and we’ll see line by line how the pointer moves with each increment ++

int arr[5] = {9,23,4,12,7};


int *ptr = arr;


cout<<*ptr<< endl; // 9
cout<<*ptr++<< endl; // 9
cout<<*++ptr<< endl; // 4
cout<<++*ptr<< endl; // 5
cout<<(*ptr)++<< endl; // 5
cout<<*ptr<< endl; // 6

The first print just outputs the start of the array. We haven’t done anything here yet. So the position of the pointer is still on 9

In the second cout we first print the current state of the pointer which is 9, and then point to the next address

in the third line we start with *++ so we point to the next address which is 4 and then we print so the output here is 4

if the ++ is before the * It means whe are incrementing the value of the variable at the current position of the pointer, so we haven’t moved positions but we did change the value at the current position. The output here is 5

here it’s similar to the previous one, since the ++ is after the pointer then we print first and then increase the value at this position. The output here is still 5

the last one is straight forward we are just printing the value at the current position.

--

--

Rick
Rick

Written by Rick

I blog about everything I learn, Digital Image Processing, Data Science, IoT, Videogame design and much more :)

No responses yet