2-Dimensional array of characters C

We have seen 2-dimensional arrays, in chapter on arrays. On the same basis we can have 2-Dimensional array of characters. Imagine them to be string stored one after another.

char names [5] [10] = { “Akshay”, “Parag”,”Raman”, “Srinivas”, “Gopal”};

Above is a 2-dimensional array of characters. The first dimension gives the number of strings in the array and the 2nd dimension gives the length of each array.
Let us write a program of exchanging names in the above array.

Example

/* Program demonstrating use of two dimensional arrays
program exchanges two names */

Output
Original: Raman Srinivas
New: : Srinivas Raman

To exchange the names we are required to exchange corresponding characters of two names. Also, since 10 characters are not used for all names, the remaining space is wasted in memory. We will see how both these drawbacks can be eliminated by using array of pointers to strings, which we will come to in chapter on pointers.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

C Topics