List

List

What is an Array of String?

The string is a collection of characters, an array of a string is an array of arrays of characters. Each string is terminated with a null character. An array of a string is one of the most common applications of two-dimensional arrays.

scanf( ) is the input function with %s format specifier to read a string as input from the terminal. But the drawback is it terminates as soon as it encounters the space. To avoid this gets( ) function which can read any number of strings including white spaces.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Sting is an array of characters terminated with the special character known as the null character (\0).

Syntax

The syntax for array of string is as follows:

Syntax

datatype name_of_the_array[size_of_elements_in_array];
char str_name[size];

Example

datatype name_of_the_array [ ] = { Elements of array };
char str_name[8] = Strings;

Str_name is the string name and the size defines the length of the string (number of characters).

A String can be defined as a one-dimensional array of characters, so an array of strings is two dimensional array of characters.

Popular Course in this category

List

C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (8,333 ratings)

Course Price

View Course


Related Courses

C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

Syntax

char str_name[size][max];

Syntax

char str_arr[2][6] = { {g,o,u,r,i,\0}, {r, a, m,\0}};

Alternatively, we can even declare it as

Syntax

char str_arr[2][6] ={gouri, ram};

From the given syntax there are two subscripts first one is for how many strings to declare and the second is to define the maximum length of characters that each string can store including the null character. C concept already explains that each character takes 1 byte of data while allocating memory, the above example of syntax occupies 2 * 6 =12 bytes of memory.

Example

char str_name[8] = {s,t,r,i,n,g,s,\0};
By the rule of initialization of array, the above declaration can be written as
char str_name[] = Strings;

0 1 2 3 4 5 6 7 Index

Variables 2000 2001 2002 2003 2004 2005 2006 2007 Address

This is a representation of how strings are allocated in memory for the above-declared string in C.

Each character in the string is having an index and address allocated to each character in the string. In the above representation, the null character (\0) is automatically placed by the C compiler at the end of every string when it initializes the above-declared array. Usually, strings are declared using double quotes as per the rules of strings initialization and when the compiler encounters double quotes it automatically appends null character at the end of the string.

From the above example as we know that the name of the array points to the 0th index and address 2000 as we already know the indexing of an array starts from 0. Therefore,

str_name + 0 points to the character s
str_name + 1 points to the character t

As the above example is for one-dimensional array so the pointer points to each character of the string.

Examples of Array String in C

Following are the examples:

Example:

#include <stdio.h>
int main()
{
char name[10];
printf("Enter the name: ");
fgets(name, sizeof(name), stdin);
printf("Name is : ");
puts(name);
return 0;
}

Output:

List

Now for two-dimensional arrays, we have the following syntax and memory allocation. For this, we can take it as row and column representation (table format).

char str_name[size][max];

In this table representation, each row (first subscript) defines as the number of strings to be stored and column (second subscript) defines the maximum length of the strings.

char str_arr[2][6] = { {g,o,u,r,i,\0}, {r, a, m,\0}};

Alternatively, we can even declare it as

Syntax:

char str_arr[2][8] ={gouri, ram};

Index

Rows

01234567
0gouri\0\0\0
1ram\0\0\0\0\0

From the above example as we know that the name of the array points to the 0th string. Therefore,

str_name + 0 points to 0th string gouri

str_name + 1 points to 1st string ram

As the above example is for two-dimensional arrays so the pointer points to each string of the array.

Example:

#include <stdio.h>
int main()
{
int i;
char name[2][8] = {
gouri,
ram
};
for (i = 0; i < 2; i++)
{
printf(String = %s \n, name + i, name + i);
}
return 0;
}

Output:

List

Functions of strings

strcpy(s1,s2); this function copies string s2 innto sting s1.
char s1[10] = gouri;
char s2 [10] = ram;
char s3 [10] ;
strcpy(s3,s2);
result => strcpy(s3,s2) : ram

strcat(s1,s2); this function concatenates strings s1 and s2 , string s2 is appended at the end of the string s1.
char s1[10] = gouri;
char s2 [10] = ram;
strcat(s1,s2);
result => strcat(s1,s2) : gouriram

strlen(s1); this function returns the length of the string s1.
char s1[10] = gouri;
strlen(s1);
result => 5

strcmp(s1,s2); This function compares both strings s1 and s2.
style="list-style-type: none;">
style="list-style-type: none;">

strchr(s1, ch); these functions find the first occurrence of the given character ch in the string s1 and the pointer points to this character in the string.

strstr(s1,s2); this finds the first occurrence of string s2 in the string s1 and the pointer points to the string s2 in the string s1.

With some invalid operations are str_arr[0] = gouri; in this operation pointer of the string is assigned to the constant pointer which is invalid and is not possible, because the name of the array is a constant pointer.

To avoid this we can assign str_arr by using strcpy(str_arr[0],gouri).

Conclusion

An array itself defines as a list of strings. From the above introduction, we can conclude that declaration and initialization of strings are different as we saw for every string the compiler appends null character when it reads the string as input. There are many string handling functions a few functions with examples are explained above. Therefore arrays of the string are as easy as arrays.

This is a guide to a Strings Array in C. Here we discuss the basics of the Array Strings, Example of Array String in C andFunctions of strings. You can alsogo through our other suggested articles to learn more

  1. String Array in C#
  2. Multidimensional Array in C
  3. 2D Arrays in C#
  4. String in C

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares

Share

Tweet

Share