C – Array of Pointers

Array of Pointers: If the array is Pointer type, it can store multiple references called Array of Pointers.

#include<stdio.h>
int main()
{
            int arr[5] = {10, 20, 30, 40, 50}, i;
            int* ptr[5];
            for(i=0 ; i<5 ; i++){
                        ptr[i] = &arr[i];
            }
            printf(“Array elements are : \n”);
            for(i=0 ; i<5 ; i++){
                        printf(“%d \n”, *ptr[i]);
            }
            return 0;
}

We can access elements without using index as follows:

#include<stdio.h>
int main()
{
            int arr[5] = {10, 20, 30, 40, 50}, i;
            int* ptr[5];
            for(i=0 ; i<5 ; i++){
                        *(ptr+i) = arr+i;
            }
           
            printf(“Array elements are : \n”);
            for(i=0 ; i<5 ; i++){
                        printf(“%d \n”, **(ptr+i));
            }
            return 0;
}

A function can return the string using char* return type.

#include<stdio.h>
char* read(void);
int main()
{
            char* name;
            name = read();
            printf(“Name returned by read function is : %s \n”, name);
            return 0;
}
char* read(void)
{
            char* name;
            printf(“Enter your name : “);
            gets(name);
            return name;   
}
Scroll to Top