Pointers and Arrays in C

Pointers and Array in C


We know that normally for accessing the element of array we use subscript with inidces like suppose a is an array we can write a[index] for access element of array.Now here we discuss about how we access the element of array using pointers.

Example 1:

Copy Code
void main()
{
    int a[5]={10,20,30,40,50};
    int *ptr1,*ptr2;
    ptr1=&a[0];
    ptr2=a;
    printf("ptr1=%d\n",*ptr1);
    printf("ptr2=%d\n",*ptr2);
    printf("Display all element of array by using pointer:\n");
    for(int i=0;i<5;i++)
    {
        printf("ptr2=%d\n",*ptr2);
        ptr2++;
    }
}

Out Put:
ptr1=10
ptr2=10
Display all element of array by using pointer:
ptr2=10
ptr2=20
ptr2=30
ptr2=40
ptr2=50

From the above example it is clear that we can access first index element by two ways by using pointer such as ptr1 and ptr2 both print same value 10, that means ptr1=&a[0]; and ptr2=a; are same.

Example 2:Display the element of array using pointer arithmetics.

Copy Code
#include <stdio.h>

void main()
{
    int i,t,a[5]={10,20,30,40,50};
    int *ptr;
    printf("Display all element of array:\n");
    for(int i=0;i<5;i++)
    {
        t=*(a+i);
        printf("%d\n",t);
    }
}

Output:

Display all element of array:
10
20
30
40
50

Pointer and two dimension array


It is same as one dimension array as define above but here the first element is &a[0][0].That means the address of zeroth row and zeroth column of the two dimensional array a is assigned to the pointer variable a.If we want to increase the pointer to point the next element of array than increase pointer ptr by 1 such as ptr++ or ptr+1 that point to a[0][1], column 0 row 1.

Example 1:Retrieve and display element of two dimension array by their index using pointers.

Copy Code
#include <stdio.h>
void main()
{
    int a[2][2]={1,2,3,4};
    int *ptr;
    ptr=&a[0][0];
    printf("First element of array=%d\n",*(ptr+0));
    printf("Second element of array=%d\n",*(ptr+1));
    printf("Third element of array=%d\n",*(ptr+2));
    printf("Forth element of array=%d\n",*(ptr+3));
}

Out Put:

First element of array=1
Second element of array=2
Third element of array=3
Forth element of array=4


Example 2:Display all element of two dimension array using pointers.

Copy Code
#include <stdio.h>
void main()
{
    int a[3][3]={1,2,3,4,5,6,7,8,9};
    int *ptr;
    int i,j;
    ptr=&a[0][0];
    
    for(i=0;i<3;i++)
    {
        for(j=0;j<3;j++)
        {         
          printf("%d",*ptr); 
          ptr=&a[i][j]+1;                    
        }
        printf("\n");
    }
}

OutPut:


Array of Pointers

Like other datatype (int,float,char etc.) pointers are also arrange in array.
Let we see how we declare integer pointer array of any size.

Syntax:

datatype *variable-name[size];

Example:

int *ptr[10];
We can access pointer array elements such as ptr[0],ptr[1],ptr[2] and so on.
If we have any integer array like int x[10],y[10],z[10];
we can write
ptr[0]=&x[0];
ptr[5]=&y[0];
ptr[6]=&z[0];
We can declare character pointer array such as:
Example:
char *name[10][5];
For initialisation we can write:
name[0]="shailu"
name[1]="C prog" and so on

Programs 1: Write a program to display the element of pointers using an array of pointers.

Copy Code
#include <stdio.h>
void main()
{
    int *ptri[10],a[10]={1,2,3,4,5,6,7,8,9,10};
    char *ptrc[5],name[5][10]={"shailu","amit","sumit","ram","shyam"};
    
    //Initialize pointer variables
    ptri[0]=&a[0];
    ptrc[0]=&name[0][0];
    ptri[1]=&a[1];
    ptrc[1]=&name[4][0];
    
    printf("Value of ptri at index 0=%d\n",*ptri[0]);
    printf("Value of ptri at index 1=%d\n",*ptri[1]);
    
    //for printing char pointer array no * is used with ptrc 
    printf("Value of ptrc at index 0=%s\n",ptrc[0]);
    printf("Value of ptrc at index 1=%s\n",ptrc[1]);
}

Output:

Value of ptri at index 0=1
Value of ptri at index 1=2
Value of ptrc at index 0=shailu
Value of ptrc at index 1=shyam

Know more about pointers


No comments:

Post a Comment