In this article arrays of pointers in c we give the information about we know that rarely the individual strings will be of equal lengths. Therefore, instead of making each row a fixed number of characters, we can make it a pointer to a string of varying length.

Array of Pointer in C:

One important use of pointer is in handling of a table of strings. Consider the following array of strings:

char name [4][20];

This says that the name is a table containing four names, each with a maximum length of 20 characters (including null character). The total storage requirements for the name table are 80 bytes.

We know that rarely the individual strings will be of equal lengths. Therefore, instead of making each row a fixed number of characters, we can make it a pointer to a string of varying length.

For example:

char *name[4] = { “Maharashtra”, ”Goa”, “Karnataka”, “Gujarat ” };

Declares name to be an array of four pointers to characters, each pointer pointing to a particular names as:

name [0] ——–> Maharashtra

name [1] ——–> Goa

name [2] ——–> Karnataka

name [3] ——–> Gujarat

This declaration allocates only 34 bytes, sufficient to hold all the characters as shown below

M A H A R A S H T R A \0
G O A \0
K A R A N A T K A \0
G U J A R A T \0

The following statement would print out all the four names:

for(i=0; i<4; i++)

printf(“\n %s”,name[i]);

To access the jth character in the ith name, we may write as

*( name [ i ] + j )

The character arrays with the rows of varying length are called ‘ragged arrays’ and are better handled by pointers.

Remember the difference between the notations *p[4] and (*p)[4]. Science * has a lower precedence than [ ], *p[ 4 ] declares p as an array of 4 pointers while (*p)[4] declares p as a pointer to an array of four elements.

Example:

Array of Pointer in C:

// Array of pointers

# include<stdio.h>

# include<conio.h>

# include<string.h>

void main()

{

char *name[4]={“Maharashtra”,”Goa”,”Karnataka”,”Gujarat”};

int i;

clrscr();

printf(“\n\n Array of Pinter: “);

for(i=0;i<4;i++)

printf(“\n %s”,name[i]);

getch();

}

Output:

Array of Pinter:

Maharashtra

Goa

Karnataka

Gujarat

Pointers and Arrays:

When an array is declared, the compiler allocates a base address and sufficient amount of storage to contain all the elements of the array in contiguous memory locations. The base address is the location of the first elements (index 0) of the array. The compiler also defines the array name as a constant pointer to the first element.

/* Write a program using pointers to compute the sum of all elements stored in an array. */

#include<stdio.h>

#include<conio.h>

void main()

{

int *p,sum=0,i;

int data[4]={3,1,5,9};

i=0;

clrscr();

p=data; // iniyializing with base addreaa of data

printf(“\n\n Elements \t Value \t Address”);

while(i<4)

{

printf(“\ndata[%d] \t%d \t%u\n”,i,*p,p);

sum=sum + (*p);    // accessing array element

i++, p++;

}

printf(“\n Sum = %d \n “, sum);

printf(“\n Address data[0] = %u \n”, &data[0]);

printf(“\n p  = %u \n “,p);

getch();

}

Output: 

Elements              Value              Address

data[0]                  3                     65516

data[1]                   1                      65518

data[2]                  5                      65520

data[3]                  9                      65522

Address data[0] = 65516

p = 65524

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

Your email address will not be published. Required fields are marked *