Pointer to Array in C


Pointer to Array in C
Pointer to array is a pointer variable that points to an array. It is also known as array pointer for short. It is usually used to reference that rows of a two dimensional array as each row of two dimensional array is an array itself.
Pointer to array means we can declare a pointer variable that can point to the whole array instead of one element of the array.
Its Syntax is,
     data_type   (*arrptr) [MAX_elements_in_row];
Where arrptr is the name of the pointer variable which will be pointing to value of type data_type and MAX_elements_in_row is a positive-valued integer expression that indicates the maximum number of elements in each row of the two dimensional array.
  In the above declaration, we notice that the pointer type (the asterisk) is placed within parentheses because of precedence. The brackets [] have greater precedence that the asterisk (*). Without the parentheses to force precedence, brackets would be evaluated first. So you would have array of pointers instead of pointer to an array.
 The statement,
   int   (*rowptr) [4];
declares a pointer to an array of 4 elements with type int.
value
of
Int
type
                                    Pointer to array containing 4 elements
Once we assign the address of an appropriate array to pointer to an array then the expression (*rowptr) yields the array and (*rowptr) [i] yields the array element at index i.
Consider the following program.
C program to show how array elements are accessed using pointer to array
#include <stdio.h>
#include <conio.h>
void main ()
{
int a[3] [4] = {{3, 4, 5, 6}, (10, 20, 30, 40}, {1, 4, 3, 9}};
int (*rowptr) [4] ;      /* pointer to array of 4 elements with type int */
int *ptr;
clrscr ();
rowptr = a;               /* Stores the address of first element */
printf (“- - - - - Elements are - - - - -\n”);
while (rowptr<(a+3))     /* As 3 rows */
{
ptr = *rowptr;    /* Stores address of first element of each row */
printf (“Row address = %u\n”,ptr);
printf (“Elements of this row - - - - :”);
while (ptr<(*rowptr+4))   /*As 4 elements in each row */
{
printf (“%d\t”, *(ptr));
ptr++;    /* Makes ptr point to next element in each row */
}
rowptr++;   /*Makes rowptr point to element after 4 elements */
printf (“\n”);
}
getch ();
}
OUTPUT
- - - - - Elements are - - - - -
Row address = 65466
Elements of this row - - - - : 3    4    5    6
row address = 65474
Elements of this row - - - - : 10    20    30    40
Row address = 65482
Elements of this row - - - - : 1    4    3    9
Explanation: In this program, the statement
    int   (*rowptr) [4];
declares a pointer to an array of 4 elements with type int.
The statement, rowptr = a; stores the address of first element of array a.
Some other Example (Program):
Example of pointer to one dimensional array.
#include <stdio.h>
#include <conio.h>
void main ()
{
int x;
int n[5] = {10, 20, 30, 40, 50);
int *p = m;    /* or  int *ptr = &m[0] */
for (x=0; x<5; x++)
{
printf (“%d\t” , *p);
p++;
}
getch ();
}
OUTPUT
10    20    30    40
Example of pointer to two dimensional arrays.
#include <stdio.h>
#include <conio.h>
void main ()
{
int x, y;
int m[2] [2] = { { 10, 20}, {30, 40} };
int (*p) [2] = m;
for (x=0; x<2; x++)
{
for (y=0; y<2; y++)
printf (“%d\t”, (*(p+x) ) [y] );
}
getch ();
}
OUTPUT
10    20    30    40

No comments

Powered by Blogger.