Pointers and Arrays are often mentioned together. Why? What really is an array? Learn how to use pointers to access arrays and create arrays of strings.





Printing the addresses in decimal:

Printing the addresses in hexadecimal:

(The addresses of the two results aren’t equivalent)

In the previous posts we saw how to get the value of each element inside an array, but what kind of value does the array itself return?

If to print an element you can use arr[index], what does just arr print?

The answer is: an address.

What is an array variable?


An array variable is a pointer to the first element of the set.

and

are equivalent.

&arr[0] is equal to arr.

This is why, when you print an array, a memory address appears on the screen.

arr is a pointer to arr[0], as you can see in the following program:

I got this result:
Result of the program
but you can get a different one: anyway the two addresses are always the same.

Accessing an array element using pointers


In the computer memory, all the elements of an array are stored one after the other, in order.

Array elements sequence

Therefore, you can access an element in an array by adding its index to the first address.

As you learnt last week, thanks to Learn C programming: Pointers | Step-by-Step explanation, you know that when you add an integer – such as the index of an element – to a pointer – such as an array -, the first is scaled to the data-type dimension of the second.

The result of the following sum, where arr is – for example – an int pointer that points to 0028FF00:

is 0028FF04 and not 0028FF01, because ( usually ) int is 4 bytes, so 1 is scaled to its size.

Using this method you can get the addresses of all the elements of an array, but how can you get their values?

Obviously, since arr+index is a pointer ( an address ) you can get the value of that element using an asterisk.

The expression arr[2] is equal to *(arr+2)

Once the program is compiled, the two methods are equal, because the machine always works with the second one: there’s no difference in performance, so feel free to use the one you prefer.

Note: you can’t change the address of an array, but you can change the value of a pointer pointing to the same address.

Why? Because a pointer variable points to another address, while an array variable is an address: you can’t change the address of a variable, but you can change the address pointed by a variable.

Examples:

Creating arrays of strings using pointers


Like every other type of variable, also pointers can form arrays.

Arrays of pointers are often used to manage arrays of strings.

The following program prints an array of strings with their corresponding addresses – where +0 is the initial one.

Each string is a pointer: the compiler will create an array of characters and automatically assign its address to a pointer of the array.



Do you want to know other methods for creating an array of strings? Check this short answer on StackOverflow

Let’s analyse today’s program


This program uses pointer to first assign to each element of an array a multiple of 4 ( including 0 ) and then to print all the values with their memory addresses.

In the first part I used directly the array variable to get the address of each element, because I didn’t have the necessity of editing it.

In the second one I wanted to use a different approach using the pointer itself as counter, so I couldn’t use the array variable, because I had to increment its address.

As always, if you want to know more about basic/previous statements check the previous posts, such as Learn C Programming: Variables | Step-by-Step Explanation.

int arr[10]

It creates an array not initialised of 10 integers.

int *p

It creates an int pointer not initialised. It’s important to specify its type, so that values added to it can be scaled correctly.

*(arr+i) = i*4

The left side of this expression its the element of the array arr with index i. The first thing that is executed is arr+i, which returns the address of the element with index i, then, thanks to the asterisk, the program gets the element itself from its address.

This line is equal to arr[i] = i*4.

p = arr

This line assigns the address of arr – which is the same address of the first element of the array – to the pointer p.

This assignment is equal to p = &arr[0]

for(int *i=(p+10); p

This loop uses the pointer p as a counter, so that when it's incremented, it:

  1. Counts the number of completed cycles
  2. Points to the next element of the array

How does it work?

It creates a temporary pointer i, that points to the 10th unit after the initial address pointed by p ( the same address of an hypothetical 11th element ): this loop has to be repeated as long as the address pointed by p is less than the one pointed by i.

At the end of each cycle, p is incremented of one unit ( the size of int, due to scaling ) and it will go from the address of the 1st element to the one of the 10th.

printf("Value:\t%d\tAddress:\t%u\n", *p, p)

This function prints the value of the element pointed by p, extracted using the asterisk, and its address in decimal notation.

Translating the program in human language


Computer, include the standard C library for inputs and outputs, stdio.h
Here’s the main body of the program
Create an array arr of 10 integers
Then, create a pointer to an integer
Create a temporary variable i equal to 0
Repeat this 10 times, incrementing i at the end of each cycle
Assign to the element at the address of arr plus i units the result of i times 4

Assign the address pointed by arr to p
Create a temporary pointer i equal to the address pointed by p plus 10 units
Repeat this until the address pointed by p is less than the one pointed by i and add one unit to p at the end of each cycle

Print "Value:", next tab space, element pointed by p, next tab space, "Address:", next tab space, address pointed by p

C and C++ comparison


c++ vs c

The main difference between the C and the C++ versions of today's program is the fact that cout automatically decides in which format printing a variable.

Therefore, while the C version prints the addresses in decimal notation, the C++ one does it in hexadecimal notation.

You can tell the program to print them in decimal form also in C++ by casting the pointer to an integer, but we haven't talked about casting yet.

Conclusion


Today you've learn two important topics of C and C++:

  • Using pointers to manage arrays
  • Using pointers to create arrays of strings

You learnt that an array variable is a pointer to the first element of the array itself.

However, there's a little difference between pointers and array variables: in fact, while a pointer has its own address and it points to the address of a different variable, an array variable is itself an address, and you can't edit an address.

This post has been quite short, compared to the latest, but nevertheless very useful.

If there's something you want to ask, some doubts you'd like to solve, feel free to write a comment below and I'll be happy to help you!

If you liked it, share it to anyone you know who may find it helpful - friends, relatives, robots, anyone.

Anyway, the post is finished, have a good day and we will see next week!

From Zephyro it’s all, Bye!

Categories: Learn C

Leave a Reply

%d bloggers like this: