Pointers and Arrays in C/C++ program

In this tutorial, we will learn about the well built relationship between pointers and arrays in C/C++ programming. Pointers are variables that are used to store the address of a variable/function and even arrays that are blocks holding sequential data. We can operate an array using pointers in a very interesting and useful manner.

Before moving ahead make sure to go through the following introductory reading about pointers:

Pointers with arrays

When we declare any array, we have to specify its data type and size. For example, an integer array named ‘array’ of size 5 will be declared as follows:

int array[5];

This means that we have indeed created 5 integer variables named array[0], array[1], array[2], array[3] and array[4]. These five integers will be stored in the memory as a block of five consecutive integers. The figure below shows the horizontal memory for example purposes.

Pointers and arrays memory pic1

For example, we are showing that array[0] is stored at 100, array[1] will be 4 bytes ahead of array[0] at 104 because integer variables need 4 bytes of memory. Thus, array[2] will be at address 108, array[3] will be at address112 and array[4] will be at address 114. . The overall size of the array will be 20 bytes(5×4) and these 20 bytes will form one consecutive block as shown in the section of the memory in which the array is stored.

Similarly, we can also show the memory horizontally from left to right as well. We will increase the address while moving from left to right. Additionally, the array[5] has the following values: {10,11,12,13,14}.

Pointers and arrays memory pic2

The following table shows the addresses and values for each index.

array[i]Located at addressValue Stored
array[0]10010
array[1]10411
array[2]10812
array[3]11213
array[4]11414

Note: The address locations are just for example purposes and do not depict exact locations.

Important: The array elements are stored in contiguous memory locations, each element occupying four bytes, since it is an integer array.

Now for observational purposes let use the horizontal representation of the memory. This time we will show the memory a little bit more extended to the right so that we can accommodate more variables. Let’s say we have an integer variable ‘x’ and its value is set to ‘100’. For example, x is located at address 200. Now if we have a pointer to integer ‘ptr’. In ‘ptr’ we want to store the address of ‘x’. This will be accomplished through the lines of code given below.

int x = 100;
int *ptr;
ptr = &x;

You can also view it in the horizontal memory segment as shown below:

Pointers and arrays memory pic3

Now if we print ptr as an output then the value in ptr would be equal to 200. You can use the following statement to print the value of ‘ptr.’ This is the address of the variable ‘x’.

printf("\n%d",ptr);

If we deference ptr and print the value stored in this location then the value will be equal to 100. You can use the following statement to print the value. Use an asterisk sign in front of the pointer variable to access the value of the variable it points to.

printf("\n%d",*ptr);

Additionally, we also know that we can increment/decrement a pointer variable by a constant. So we can do something like this:

ptr=ptr+1;
printf(ptr);

This will take us to the address of the next integer. As integers take up 4 bytes of memory so now the next index will be at 204. If we want to print ‘ptr’ then the output should be 204.

If we try to dereference ‘ptr’, and try to print *ptr then we do not know the value at this address. We can not say what will be printed. We know that ‘x’ is at address 200 but do not know the value of the variable at the next address (204) location.

Pointers and arrays memory pic4

Assigning an array to a pointer

However, this can be known when working with arrays and pointers. For this integer array (array[5]), also shown in the figure located at address 100. We will now declare a pointer to an integer called ‘ptr.’ Next, we will store the address of the first element of the array in this pointer. This will be achieved by putting an ampersand operator(&) in front of array[0]. Then printing ‘ptr’ will give us the output 100.

int array[5]={10,11,12,13,14};
int *ptr;
ptr = &array[0];
printf("\n%d",ptr);

Printing *ptr will give 10 in the output.

printf("\n%d",*ptr);

If I want to print (ptr+1) then the address would be 204 and if i try to dereference (p+1) and try to print this value then it will be ’11.’

printf("\n%d",ptr+1);
printf("\n%d",*(ptr+1));

Similarly, if we wanted to access the third element of the array we could print (ptr+2).

Thus, using pointer arithmetic makes sense in the case of arrays, as we know the value in the adjacent location.

Accessing the address of the first element of an array

One more property of the array is that by just using the name of the array we can access the address of the first element of the array. Then ‘array’ gives us the pointer to the first element in the array.

For example:

 ptr =array; 

In fact we do not even need to take this address in another pointer variable. If we simply print ‘array’ then this gives us nothing but the address of the first element of the array which is 100 in our case.

int array[5]={10,11,12,13,14};
int *ptr;
ptr = array;
printf("\n%d",array);

Assigning an array to a pointer is equal to assigning the memory address of the first value in the array to the pointer

How to get the memory address and value of an array element?

  • To obtain the value of the first element of the array we will have to deference ‘array’ as shown below. This will give us the value of the first element of the array which is 10 in our case.
printf("\n%d",*array);
  • If we want to print (array+1) then this will give us the address of the second element in the array i.e.104 and *(a+1) will give us the value of the second element of the array i.e. ’11’ in our case.
printf("\n%d",array+1);
printf("\n%d",*(array+1));

For an element in the array at index ‘i’, we can retrieve the address of this particular element in the memory using either &array[i] or (array+i). These two will give us the address of array[i].

The value of array[i] can be retrieved using either array[i] or *(array+i). This is an important concept. Both statements are equivalent.

  • The address of the first element in the array can also be called the base address. Simply using the variable name e.g. ‘array’ gives us the base address of the array.

Let us look at some example C codes to further understand the concepts of pointers with arrays.

Subscript notation vs Pointers. Which one to choose when accessing values?

As you noticed, we used two different methods to locate the memory addresses of the elements of the array as well as accessing the value of the elements. We used both subscript notation and pointers to access the values of elements. Although we obtained the same result in either case but using one of them has a slightly higher advantage over the other, depending upon the way you want to access the elements.

  • If you want to access elements in an array in a particular fixed order e.g. from beginning to end or using some other logic, then using pointers is clearly a better option. Moreover, using pointers to access elements in arrays is more faster than using subscript notation.
  • However, if there is no fixed logic being used to access the elements then it is very easy to use the subscript notation instead of working with pointers. It is simpler and easier to program.

Example Codes for pointers with arrays

In our first example program, we have an integer array called ‘num’. If we simply print ‘num’ then it should give us the address of the first element in the array. We can also obtain the address of the first element in the array by using the ampersand operator in front of num[0]. Printing num[0] will print the first element in the array. Moreover, we can also access the first element in the array by using an asterisk operator in front of the variable name ‘num.’

#include <stdio.h>
int main(){
int num[]={1,2,3,4,5,6};
printf("Address of first element: %d\n",num);
printf("Address of first element: %d\n",&num[0]);
printf("Value of first element: %d\n",num[0]);
printf("Value of first element: %d\n",*num);
    
}

Code Output

Now let’s see the code output. After the compilation of the above code, you will get the following output.

Pointers and arrays memory output pic1

As you can see the first two lines are the same. They are giving us the address of the first element in the array. The second two lines are also same and give us the value of the first element of the array. In our case, it is ‘1’.

  • To find the address of the first element, we can either use name of array e.g. num or &num[0].
  • To find the value of the first element we can either use num[0] or *num.

Using loop() to access all elements’ locations and values

In fact, if we run a loop() like this for 0 to index 6 then we can print addresses of the elements at index i as &A[i] or (A+i). Similarly, we can print the value of the ith element as A[i] or *(A+i). The following code does that.

#include <stdio.h>
int main(){
int num[]={1,2,3,4,5,6};
int i;
for (i=0;i<6;i++){
printf("Address: %d\n",&num[i]);
printf("Address: %d\n",num+i);
printf("Value: %d\n",num[i]);
printf("Value: %d\n",*(num+i));
printf("\n");
    
}
}

Code output

Now let’s see the code output. After the compilation of the above code, you will get the following output.

Pointers and arrays memory output pic2

Acessing Array Elements through a Pointer

Notice, the address printed for a particular index are same and the value at that index is also the same. Also, note that as the array ‘num’ is of integer data type that takes 4 bytes of memory per variable hence the addresses of consecutive elements differ by 4.

Using the variable name ‘num’ returns a pointer to the base address or the address of the first element. We can equate the variable ‘num’ against some pointer variable ‘ptr.’

int *ptr = num;

However, we can not increment/decrement the value of the array num like num = num+1; or num = num-1; This will give compilation error. However, we can increment the pointer like ptr = ptr + 1; once we assign num to some pointer variable but incrementing/decrementing ‘num’ itself would be invalid.

int *ptr =num;
ptr++; 

This was an introduction to how arrays are stored in memory, how addresses can be manipulated, and how we can access the values using pointers.

#include <stdio.h>
int main(){
int num[]={1,2,3,4,5,6};
int *ptr = num;
for (int i=0;i<6;i++){
printf("Address of num[i]: %d",ptr);
printf(" Value of num[i]: %d",*ptr);
printf("\n");
ptr++;
}
Address of num[i]: 424745984 Value of num[i]: 1
Address of num[i]: 424745988 Value of num[i]: 2
Address of num[i]: 424745992 Value of num[i]: 3
Address of num[i]: 424745996 Value of num[i]: 4
Address of num[i]: 424746000 Value of num[i]: 5
Address of num[i]: 424746004 Value of num[i]: 6

You may like to read other pointer tutorials:

Leave a Comment