String Arrays in C
In this lesson we will study how to declare Static String Arrays in C language.
We will see that there are two ways to define a string array in C: static arrays and jagged arrays. Static arrays are simpler to manage, but waste space. Jagged arrays, on the other hand, are more efficient, but cannot be modified.
Static String Arrays
One of the needs that often arises in C programming is to store an array of strings. In this lesson we focus on the static case, that is, when the number of strings is known in advance. In future lessons, when we study dynamic memory management, we will see how to handle string arrays of variable length.
To declare a string array in C, we must remember that a string is, in reality, an array of characters terminated by a null character '\0'. Therefore, a string array is nothing more than a multidimensional string array.
Therefore, to define a multidimensional array of char (that is, a string array), we can use the following syntax:
char days[][10] = {
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
};
In this example, days is a string array, each of which can contain up to 9 characters (10, considering the null terminator). The days array is initialized with 7 strings, each of which represents a day of the week.
Note that, in defining the array, we omitted the dimension of the first dimension. This is possible because the compiler can deduce the dimension of the first dimension from the array initialization. However, we were forced to specify the dimension of the second dimension, since the compiler cannot deduce it.
In memory, our string array will be nothing more than a character matrix. In particular, a matrix composed of 7 rows and 10 columns, where each row represents a string. To better understand the memory layout, let's observe the following figure:
From the figure, one thing immediately becomes clear: strings that contain less than 10 characters (including the terminator) are filled with terminators until they reach the maximum length. In technical jargon, this is called padding. This is necessary to ensure that each string has the same length in memory. However, it represents a waste of space.
In C, this inefficiency is common when working with strings, since many string collections must contain strings of variable length. This is the main difference compared to arrays containing fixed-length data, such as primitive types themselves, where the size is known in advance.
Static String Arrays
A static string array in C is a multidimensional character array, where each row represents a string. The dimension of the first dimension can be omitted, while the dimension of the second dimension must be specified. The dimension of the second dimension represents the maximum length of each string.
The syntax for declaring a static string array is as follows:
char array[][N] = {
"string1",
"string2",
"string3",
...
};
where N represents the maximum length of each string.
Jagged String Arrays
The problem of space waste seen above can be solved with jagged arrays, also called irregular arrays or irregular matrices.
Natively, the C language does not support jagged arrays. However, we can simulate a jagged array using an array of character pointers. In this way, we can create a variable-length string array, without wasting space.
Returning to the previous example, we can modify the code as follows:
char *days[] = {
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
};
In this case, days is an array of character pointers. Each element of the array points to a string. The memory layout is represented in the figure:
In this case, there is no waste of space, since each string occupies only the space necessary for its characters. However, we must be careful not to modify the strings, since they are stored in a read-only section of memory.
In this case, we have created a static jagged array. We cannot modify the number of elements and we cannot modify the strings without causing undesired effects. In the lessons on dynamic memory management, we will see how to create and modify them dynamically.
Static Jagged String Arrays
A static jagged string array in C is an array of character pointers, where each pointer points to a string.
The syntax for declaring a static jagged string array is as follows:
char *array[] = {
"string1",
"string2",
"string3",
...
};
In Summary
In this lesson we have seen how to define string arrays in C. We have analyzed two approaches: static arrays and jagged arrays. Static arrays are simpler to manage, but waste space. Jagged arrays, on the other hand, are more efficient, but cannot be modified.
In the next lesson we will see a very important application of string arrays: access to command line arguments.