C

Example Of A Pointer And Array In C Programming

In this tutorial, we’ll look at how to use pointers and arrays in a C program. I recommend that you read the Array and Pointer tutorials before proceeding with this guide so that you can understand the concept explained here.

An easy example of printing the address of array elements

#include <stdio.h>
int main( )
{
   int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ;
   /* for loop to print value and address of each element of array*/
   for ( int i = 0 ; i < 7 ; i++ )
   {
      /* The correct way of displaying the address would be using %p format
       * specifier like this:
       * printf("val[%d]: value is %d and address is %p\n", i, val[i], &val[i]);
       * Just to demonstrate that the array elements are stored in contiguous
       * locations, I m displaying the addresses in integer
       */
      printf("val[%d]: value is %d and address is %d\n", i, val[i], &val[i]);
   }
   return 0;
}

Output:

val[0]: value is 11 and address is 1423453232
val[1]: value is 22 and address is 1423453236
val[2]: value is 33 and address is 1423453240
val[3]: value is 44 and address is 1423453244
val[4]: value is 55 and address is 1423453248
val[5]: value is 66 and address is 1423453252
val[6]: value is 77 and address is 1423453256

Because an integer is 4 bytes long, there is a difference of 4 bytes between each element. That is, all of the elements are stored in memory in consecutive contiguous memory locations (See the diagram below)

In the preceding example, I used &val[i] to get the address of the array’s ith element. To get the address, we can also use a pointer variable instead of the ampersand (&).

Example of an Array and a Pointer in C

#include <stdio.h>
int main( )
{
   /*Pointer variable*/
   int *p;

   /*Array declaration*/
   int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ;

   /* Assigning the address of val[0] the pointer
    * You can also write like this:
    * p = var;
    * because array name represents the address of the first element
    */
   p = &val[0];

   for ( int i = 0 ; i<7 ; i++ )
   {
      printf("val[%d]: value is %d and address is %p\n", i, *p, p);
      /* Incrementing the pointer so that it points to next element
       * on every increment.
       */
      p++;
   }
   return 0;
}

Output:

val[0]: value is 11 and address is 0x7fff51472c30
val[1]: value is 22 and address is 0x7fff51472c34
val[2]: value is 33 and address is 0x7fff51472c38
val[3]: value is 44 and address is 0x7fff51472c3c
val[4]: value is 55 and address is 0x7fff51472c40
val[5]: value is 66 and address is 0x7fff51472c44
val[6]: value is 77 and address is 0x7fff51472c48

Important Notes:
1) When using pointers with arrays, the pointer’s data type must match the array’s data type.
2) You can also use the array name to initialize the pointer, as shown below:

p = var;

because the array name alone is equivalent to the array’s base address

val==&val[0];

3) Within the loop, the increment operation (p++) on the pointer variable is performed to obtain the next location (next element’s location); this arithmetic is the same for all types of arrays (for all data types double, char, int, etc.), despite the fact that the bytes consumed by each data type differ.
The logic of pointers
You should have understood the logic in the preceding code, so now it’s time to experiment with some pointer arithmetic and expressions.

if p = &val[0] which means
*p ==val[0]
(p+1) == &val[2]  & *(p+1) == val[2]
(p+2) == &val[3]  & *(p+2) == val[3]
(p+n) == &val[n+1) & *(p+n) == val[n+1]

Using this logic, we can better rewrite our code as follows:

#include <stdio.h>
int main( )
{
   int *p;
   int val[7] = { 11, 22, 33, 44, 55, 66, 77 } ;
   p = val;
   for ( int i = 0 ; i<7 ; i++ )
   {
      printf("val[%d]: value is %d and address is %p\n", i, *(p+i), (p+i));
   }
   return 0;
}

The p++ statement is unnecessary in this program.

Related Articles

Leave a Reply

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

Back to top button