Wednesday 16 September 2020

Pointer and Array Programs.

 


Pointer to an Array | Array Pointer


Pointer to Array

Consider the following program:

#include<stdio.h>
  
int main()
{
  int arr[5] = { 1, 2, 3, 4, 5 };
  int *ptr = arr;
  
  printf("%p\n", ptr);
  return 0;
}

In this program, we have a pointer ptr that points to the 0th element of the array. Similarly, we can also declare a pointer that can point to whole array instead of only one element of the array. This pointer is useful when talking about multidimensional arrays.
Syntax:

data_type (*var_name)[size_of_array];

Example:


int (*ptr)[10];

Here ptr is pointer that can point to an array of 10 integers. Since subscript have higher precedence than indirection, it is necessary to enclose the indirection operator and pointer name inside parentheses. Here the type of ptr is ‘pointer to an array of 10 integers’.

No comments:

Post a Comment