C Tutorial – Pointers

Pointers vs Arrays : are the same?

In this post we will take a look at the difference between Pointers and Arrays.

Pointers and Arrays are not the same thing but there are a lot of common syntax that you can use

The biggest thing that they have in common is that both are sequence of memory address that you can access.

Let’s have a look at the array: you know that when you pass a variable by value to a function what happen is that the C Compiler makes a copy of the local variable in the stack and pass the copy to the function. Although both variables have the same value their memory address is different: in other words they are located in a different section of the memory.

Now when you pass one array as a value to a function the C compiler does not copy the whole array in the stack: that would be very inefficient. The C compiler will copy only the address of the first element.

That is a bit similar to what happen with a pointer where the C compiler move through the memory blocks starting from the first bloc.

Let’s have a look at an example. We declare an array of for elements and we fill with some integer values.

int myarr[4];
myarr[0]=20;
myarr[1]=40;
myarr[2]=60;
myarr[3]=80;

Now let’s print the memory location of the array. There are several way of doing that. We can do this way

 printf("This is the memorry address of array using myarr     %p\n",myarr);

Notice please: We have an integer array but we are asking to print the memory address with printf using the a format specifier p!:Format specifier p is used to “writes an implementation defined character sequence defining a pointer” (source https://www.programiz.com)

Now we can also print the memory address of the integer pointer using a different syntax

printf("This is the memorry address of array using &myarr    %p\n",&myarr);
    printf("This is the memorry address of array using &myarr[0] %p\n",&myarr[0]);
    printf("This is the memorry address of array using &myarr[1] %p\n",&myarr[1]);
    printf("This is the memorry address of array using &myarr[2] %p\n",&myarr[2]);
    printf("This is the memorry address of array using &myarr[3] %p\n",&myarr[3]);

This will return the following values. Notice please: that if we do not pass the element of the array the Compiler (first printf statement) will return the address of the first element (this is similar to what happen with a pointer)

Now let’s suppose we want to have a pointer to the array

 int *pointerArray;
 pointerArray=myarr;

Let’s print the memory address of the pointer at different location

printf("\n");
    printf("------How to move between memory address of the pointerArray---------:\n");
    printf("This is the memorry address of the pointerArray base      %p\n",pointerArray);
    printf("This is the memorry address of the pointerArray+1         %p\n",pointerArray+1);
    printf("This is the memorry address of the pointerArray+2         %p\n",pointerArray+2);
    printf("This is the memorry address of the pointerArray+3         %p\n",pointerArray+3);

Those instruction will print all the memory blocs of the array

If I want to know the value of first element of array I can de-reference with the following standard command to de-reference the pointer

printf("This is the memorry address of the *(pointerArray+1)      %d\n", *(pointerArray+1) );

But I can also use (notice please) the array’s way to de-reference the pointer

printf("This is the memorry address of the pointerArray[1]        %d\n",pointerArray[1]);

The result will be exactly equal, in this example 40.

Once I have a pointer we can say that pointer is equal to an array. This expression is valid. However we cannot say that array is equal to pointer.

Here is the code of the example above

Leave a Reply

Your email address will not be published. Required fields are marked *