Array of Strings in C (original) (raw)
Last Updated : 10 Jan, 2025
In C, an array of strings is a 2D array where each row contains a sequence of characters terminated by a '\0' NULL character (strings). It is used to store multiple strings in a single array.
Let's take a look at an example:
C `
#include <stdio.h>
int main() {
// Creating array of strings for 3 strings
// with max length of each string as 10
char arr[3][10] = {"Geek", "Geeks",
"Geekfor"};
for (int i = 0; i < 3; i++)
printf("%s\n", arr[i]);
return 0;
}
`
An array of strings allows you to store and manipulate text in C.
**Syntax of Array of Strings
char arr_name[**r][m**] = {s1, s2, ...., sn};
Here,
- **arr_name: Name of the variable.
- **r: Maximum number of strings to be stored in the array
- **m: Maximum number of character values that can be stored in each string.
- **s1, s2, ...., sn: Strings to be stored.
Invalid Operations in Arrays of Strings
We can't directly change or assign the values to an array of strings in C.
**Example:
char arr[3][10] = {"Geek", "Geeks", "Geekfor"};
**arr[0] = "GFG";
// This will give an Error that says assignment to expression with an array type.
To change values we can use **strcpy() function in C:
**strcpy(arr[0],"GFG");
// This will copy the value to the arr[0].
How Array of Strings are Represented in Memory?
Consider the array of string:
char **arr[3][10] = {"Geek", "Geeks", "Geekfor"};
It will be stored in the format shown below:
We have 3 rows and 10 columns specified in our array of string which means that each string can be as long as 1o characters. But as we can see, there are many strings that takes less space than 10 but still allocated the same space due to the property of 2D array. It leads to so much wastage of space. But we can avoid this wastage in our program by using array of pointers to strings.
Array of Strings Using Pointers
In C we can use an Array of pointers instead of having a 2-Dimensional character array. It is a single-dimensional array of Pointers where pointer to the first character of the string literal is stored.
Let's take a look at an example
C `
#include <stdio.h>
int main() {
// Array of pointers to strings char *arr[] = {"Geek", "Geeks", "Geekfor"};
for (int i = 0; i < 3; i++) printf("%s\n", arr[i]); return 0; }
`
**Syntax
**char *arr[] = { "Geek", "Geeks", "Geekfor" };
- **char *: It indicates that each element in the array is a pointer to a character (i.e., each points to a string).
- **arr[]: Declares an array of pointers, where the size is determined by the number of strings in the initialization list.
- **= { "Geek", "Geeks", "Geekfor" }: Initializes the array, with each pointer pointing to a string: arr[0] points to "Geek", arr[1] to "Geeks", and arr[2] to "Geekfor".
How Array of String Pointers Represented in Memory?
Unlike the 2D array of characters, array of pointer to strings store the pointer to the array. So, each string is only take the required amount of memory. Though some extra space is required to store the pointers.
**arr is an array of pointers, each pointing to a string. The strings are stored in memory, and arr holds their addresses, making this method memory-efficient and flexible for variable-length strings.