by Dinesh Thakur Category: Puppetting On Strings

A string is defined to be an array of characters. If it is desired to have an array of strings such as a list of names, it becomes similar to an array whose elements are also arrays. Therefore, an array of strings is a two-dimensional array; it may be declared as below.

char identiner [i][j];

In pointer notation, an array of strings may be declared as given below.

char* identiner [i];

Here i denotes the number of rows or number of strings and} represents the elements in rows, i.e., the number of characters in each string. In Program, an array of three strings is declared and initialized as a two-dimensional array or an array of three strings (arrays) each of 8 characters as given below.

char Name [3][8] = {"Ears", "Eyes", "Arms"};

The above array of strings may also be declared as an array of pointers, each pointer points to one array. The declaration and initialization is done in the following manner:

char* name[3] ={"Ears", "Eyes", "Arms"};

Program presents an example of an array of strings.

#include <stdio.h>

void main()
{
  int i;
  char Name [3][8] = { "Ears", "Eyes", "Arms"};
  char* name[3] = { "Ears", "Eyes", "Arms"};
  clrscr();
  printf("Size of Name= %d\n", sizeof(Name));
  printf("The actual sizes of strings are: ");
  printf("%u\t%u\t%u\n", sizeof("Ears"), sizeof("Eyes"), sizeof("Arms"));
  for (i =0; i<3; i++)
    printf("name[%d] = %s\t Name[%d] = %s\n", i, name[i], i, Name[i] );
}

Arrays of Strings in C



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.