Programming in C: Unit III (b): Pointers

Arrays of Pointers

with Example C Programs

An array of pointers can be declared as int *ptr [10];The above statement declares an array of 10 pointers where each of the pointer points to an integer variable.

ARRAYS OF POINTERS

An array of pointers can be declared as

int *ptr [10];

The above statement declares an array of 10 pointers where each of the pointer points to an integer variable. For example, look at the code given below.

int *ptr [10];

int p = 1, q = 2, r = 3, s = 4, t = 5;

ptr[0] = &p;

ptr[1] = &q;

ptr [2] = &r;

ptr [3] = &s;

ptr [4] = &t

Can you tell what will be the output of the following statement?

printf("\n %d", *ptr [3]);

The output will be 4 because ptr [3] stores the address of integer variable and *ptr [3] will therefore print the value of s that i value t is 4. Now look at ok at another code in which we store the address of three individual arrays in the array of pointers.

main()

{

int arr1 []={1,2,3,4,5);

int arr2 []=(0,2,4,6,8};

int arr3 []={1,3,5,7,9};

int *parr [3] = (arr1, arr2, arr3);

int i;

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

printf("%d", *parr[i]);

}

Output

1 0 1

Surprised with this output? Try to understand the concept. In the for loop, parr [0] stores the base address of arr1 (or, &arr1 [0]). So writing *parr [0] will print the value stored at &arr1 [0]. Same is the case with *parr [1] and *parr [2].

Now consider an array of character pointers that points to strings.

char *ptr [3];

In the ptr array, each element is a character pointer. Therefore, we can assign character pointers to the elements of the array. For example,

ptr[0] = str;

Another way to initialize an array of characters with three strings can be given as,

char *ptr[3] = {"Janak", "Raj", "Paul"};

Here, ptr [0] is Janak, ptr[1] is Raj, and ptr [2] is Paul.

The memory layout of ptr can be given as shown in Figure 7.7. It requires only 15 bytes to store the three strings.

However, char str[3] [10] = {"Janak", "Raj", "Paul"}; will behave in the same way as an array of characters. The only difference is the memory layout (Figure 7.8). While the array of pointers needs only 15 bytes of storage, str will reserve 30 bytes in memory, despite the fact that some memory locations will be reserved but not utilized.

Look at the following program that uses an array of character pointer to display the name of the day corresponding to the number.

#include <stdio.h>

char *day_of_week (int);

main()

{

int day_num;

char *day;

printf("\n Enter the day from 1 to 7: ");

scanf("%d", &day_num);

day = day_of_week (day_num);

if (day)

printf("%s", day);

else

printf("\n Invalid Day");

}

char *day_of_week (int day)

{

char *week_day [7] = {"SUNDAY", "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};

if (day>=1 | | day<=7)

return week_day [day-1];

else

return NULL;

}

Output

Enter the day from 1 to 7: 3

TUESDAY

An array of pointers whose elements point to arrays of varying sizes is called a ragged array. Therefore, array of pointers week_day and ptr in the above discussion are better known as ragged arrays.

Programming in C: Unit III (b): Pointers : Tag: : with Example C Programs - Arrays of Pointers