An Array Of Strings In C

int main

{

int i, arr[5] = {1, 2, 4, 2, 4};

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

{

printf("%d ", arr[i]);

}

}

Example


Output:

1 2 4 2 4

Example


In C, a Character and a String are separate data types, unlike other programming languages like Python. A String is a collection of Characters. Hence, to define a String, we use a Character Array:

include<stdio.h>

int main

{

char str[8];

printf("Enter a String: ");

scanf("%s", &str);

printf("%s", str);

}

Example


Output:

Enter a String: Hello

Hello

Example


Now, we want to create an Array of Strings which means we are trying to create an Array of Character Arrays. We have two ways we can do this:

- Using Two-dimensional Arrays

- Using Pointers

### Using Two-dimensional Arrays:

Creating a String Array is one of the applications of two-dimensional Arrays. To get a picture of the arrangement, observe the below representation:

For suppose we want to create an Array of 3 Strings of size 5:

Every String in a String Array must terminate with a null Character. It is the property of a String in C.

Syntax to create a 2D Array:

Data_type namerows = {{values in row 1}, {values in row 2}…};

Example


Syntax to create a String Array:

char Arrayrows = {"String1", "String2"...};

Example


Now, let us create an example String Array:

- Observe that when we assign the number of rows and columns, we need to consider the Null Character to the length.

include<stdio.h>

int main

{

int i;

char Array3 = {"Black", "Blame", "Block"};

printf("String Array: \n");

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

{

printf("%s\n", Array[i]);

}

return 0;

}

Example


Output:

String Array:

Black

Blame

Block

Example


- char Array[3][6] = {"Black", "Blame", "Black"} -> {{'B', 'l', 'a', 'c', 'k', '\0'}, {'B', 'l', 'a', 'm', 'e', '\0'}, {'B', 'l', 'a', 'c', 'k', '\0'}}

- We cannot directly manipulate the Strings in the Array as a String is an immutable data type. The compiler raises an error:

char Array[0] = "Hello";

Example


Output:

[Error] assignment to expression with Array type

Example


- We can use the strcpy() function to copy the value by importing the String header file:

char Array3 = {"Black", "Blame", "Block"};

strcpy(Array[0], "Hello");

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

{

printf("%s\n", Array[i]);

}

Example


Output:

String Array:

Hello

Blame

Block

Example


The Disadvantage of using 2D Arrays:

Suppose we want to store 4 Strings in an Array: {"Java", "T", "point", "Logic Practice"}. We will store the Strings like this:

- The number of rows will be equal to the number of Strings, but the number of columns will equal the length of the longest String.

- The memory allocated to all the Strings will be the size of the longest String, causing " Memory wastage ".

- The orange part in the above representation is the memory wasted.

### Using Pointers:

By using Pointers, we can avoid the Disadvantage of Memory wastage. But how do we do this?

We need to create an Array of Pointers pointing to Strings. Hence, we need to create an Array of type " char* ". This way, all the Strings are stored elsewhere in the exactly needed memory, and the Pointers in the Array point to those memory locations causing no memory wastage. More specifically, the Pointers in the Array point to the first Character of the Strings.

Syntax to create an Array of Pointers:

Data Type* name[] = {"Value 1", "Value 2"…};

Syntax to create an Array of String Pointers:

char* Array[] = {"String 1", "String 2"…};

Representation:

Now, let us create an example String Array:

include<stdio.h>

include<string.h>

int main

{

int i;

char* Array = {"HI", "UP", "AT"};

printf("String Array:\n");

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

{

printf("%s\n", Array[i]);

}

return 0;

}

Example


Output:

String Array:

HI

UP

AT

Example


### Summary:

We cannot create a String Array like a normal one, as a String is an Array of Characters. We have two ways to do this:

1. Using a Two-Dimensional Array:

The Disadvantage of using this way is " Memory wastage ," as the memory allocated to every String in the Array will be the memory required to store the longest String of the Array.

2. Using Pointers:

Using Pointers, we create a single-dimensional Array of Pointers pointing to Strings. Following this method can eliminate the "Memory wastage" Disadvantage.

Input Required

This code uses input(). Please provide values below: