C programming arrays and pointers

In this tutorial, you will learn in-depth about C programming arrays and pointers with their relation and difference.

c programming pointer and arays

Arrays and Pointers in C

Pointers and Arrays are kind of similar in C programming. As we all know that array is a collection of items of similar data types. And the items in an array are stored in contagious memory locations in computers memory heap. Now if an array is declared like:

int a[5];

Now in above declaration of the array, if the first element of array a[0] is stored at memory location 0x16 (say) then the second element a[1] of array will be stored at the memory location adjacent to it which is 0x16 as integer occupies 4 bytes of memory.

In computers memory, it would look like

c array memory address

In C programming, the name of an array always points to the base address i.e above a will refer to the address 0x12. So &a[0] and ‘a’ are equal.

That is why arrays and pointers are analogous in many ways.

Relation between array and pointer


Consider following declaration

int *p;
int a[5];
p = a;

Now in above declaration

&a[0] is equivalent to pointer variable p and a[0] is equivalent to *p

&a[1] is equivalent to p+1 and a[1] is equivalent to *p+1

[adsense1]

 

Analyze following operations:

p == &a[ 0 ];    //name of the array is the address of the first element
p + 2 == &a[ 2 ];     //same address
*p == a[ 0 ];        //same value
*(p + 2) == a[ 2 ];  //same value
*p + 3              //3 is added to the value of first element
*(p + 3)            //points to the third element

array and pointer

Example: C program to print sum of 2 numbers using pointer to an array

#include <stdio.h>
int main()
{
  int i, x[2], sum = 0;
  int *p;
  p = x; //assign the base address
  printf("Enter the number:");
  for( i = 0; i < 2; i++ )
  {
      scanf("%d",( p + i ));
      sum += *(p+i);        // *(p+i) equals x[i]
  }
  printf("Sum = %d", sum);
  return 0;
}

Output

Enter the numbers: 1
2
sum = 3

Difference between pointers and arrays in C programming


As we know that, string are series of characters that are stored in an array. The name of the array acts as a pointer to the base value.

Similarly, pointer variable also points to the specific location in the memory.

For example, we have to store “trytoprogram” in the computer memory.

This can be done either by storing it in an array or simply storing it somewhere in computer memory and assigning the address of the string to a char pointer.

char str[ ] = "trytoprogram";
char *ptr = "trytoprogram";

Here, we have used two different methods to store “trytoprogram” as discussed above.

Although both of these look same, there is a subtle difference in usage.

How char str[ ] and *ptr works ?


Let’s explore the difference between pointers and strings in c programming.

#include <stdio.h>

int main ()
{
   char str1[ ] = "trytoprogram";
   char str2[ 50 ];

   char *ptr1 = "hello world";
   char *ptr2;

   //look carefully
   str2 = str1; //error
   ptr2 = ptr1; //valid

   str1 = "program"; //error because it is defined and initialized already

   ptr1 = "program"; //valid and works

   return 0;
}

Explanation

Let’s discuss line by line of the above program.

We have defined two character str1 & str2 type array and initialize str1. Similarly, there are two char type pointer ptr1 and ptr2.

Now, we can clearly see from the above example that we cannot assign a string to other. However, we can easily assign pointer to another pointer.

The another difference is that we cannot initialize a string to another set of characters once it has been defined and initialized. But this is totally valid in the case of char pointers.