Pointer to an array

Pointer is a variable that holds the address of another variable of the same datatype and an array is a variable that can hold multiple values but of a similar type (homogeneous).

Thus, a pointer variable can store the address of an array as even array is a variable.

Example:

int arr[5] = {1, 2, 3, 4, 5};

Please refer to the topics below for a clear understanding before reading further:

Pictorial Representation of an array:

Suppose the base(starting) address is 1000 and each integer requires 4 bytes, the five elements will be stored as follows:

Elementarr [0]arr [1]arr [2]arr[3]arr [4]
Address:10001004100810121016

Variable arr will store the base address and it is a constant pointer (the base address of an array cannot be changed, it is constant).

arr has two significances:

  • This is the name of the array.
  • It acts as a pointer pointing to the first element of an array.

arr is equal to &arr[0] by default.

Pointer to an array:

Since the array is a variable, hence a pointer can be used to store the address of an array that is a pointer can be used to point to an array.

Hence the pointer variable can be used to access the elements of an array.

C++ Pointers and Arrays

Sample program:

#include <stdio.h>

int main ()
{
   int arr[5]= {1, 3, 3, 4, 5};
   int i = 0;
   int *ptr = &arr[0]; // int *ptr = arr; ==> pointer pointing to an array
   printf ("\n The elements of the array are:\n");
   for (i = 0; i <5; i++)
     printf ("%d ", *(ptr + i));

   /* Even this works as arr is the base address
   for (i = 0; i < 5; i++)
     printf ("%d ", *(arr + i)); */

   /* Even this works
   for (i =0;i <5; i++)
     printf ("%d ", arr[i]); */

   return 0;
}

Output:

The elements of the array are:
1 3 3 4 5 

Pointer to Multidimensional array:

The pointer can also point to a multidimensional array. In arr[row][col], arr will again give the base address of this array, even arr + 0 + 0 will also give the base address that is the address of arr[0] [0].

Generic syntax to access the element of 2-D array using pointer:

*(*(arr + i) + j)

Pointer and character string:

Pointer variable of char type can also be used to point to string and printf() and fputs() can be used to print the string.

char *str = "tech";

str is a pointer to the string and also the name of the string. Therefore, we do not need to dereference using * operator.

The difference between char *str and char str[ ]

  • The basic difference is that in the case of char *str, str is a pointer of char type which points to a constant string(anonymous array of 5 characters) whereas, in char str1[ ], the str1 is an array of character types that is a memory location of 5 characters known by name str1.
  • If str is a pointer it can point to another string that is the address can be changed but the content cannot be changed(pointer to constant) as it is stored in the read-only section of memory.
  • If str is an array, it means arr is nothing but the base address of an array that cannot be changed(constant pointer). Hence in this case the address cannot be changed but the content can be changed.

Example:

#include <stdio.h>

int main ()
{
   char *str = "tech";
   char str1[] ="tech;

   str = "access"; // Valid, it can point to some other string
   str[0] = 'z'; //Invlaid as it is stored in the read-only section of the memory (pointer to constant)
 
  str1 = "access"; // Invalid as the base address of the array cannot be changed (constant pointer);
  str1[3] = "z'; //valid;
  return 0;
}

Please refer to the constant pointer and pointer to constant for details.

Relevant Topics:



Categories: C Language

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: