Array of strings in C Language

 

An array of strings are multiple strings, stored in the form of the table. Declaring an array of strings is same as strings, except it will have an additional dimension to store the number of strings.

Syntax is as follows:

char array-name[size][size];
For example,
char names[5][10];

 

where names is the name of the character array and the constant in first square brackets will gives number of string we are going to store, and the value in second square bracket will gives the maximum length of the string.

char names [3][10] = {“martin”, “phil”, “collins”};

It can be represented by a two-dimensional array of size[3][10] as shown below:

0 1 2 3 4 5 6 7 8 9
m a r t i n \0
p h i l \0
c o l l i n s \0

 

Write a program to initializes 3 names in an array of strings and display them on to monitor

/* Program that initializes 3 names in an <em>array of strings</em> and display them on to monitor.*/

#include <stdio.h>

main()

{

int n;

char names[3][10] = {“Alex”, “Phillip”, “Collins” };

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

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

}

OUTPUT

Alex
Phillip
Collins

 

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: