Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
An array is a linear data structure that is a collection of elements of similar data types. Array provides an easy way to access and modify the elements. Array stores data in a continuous memory allocation. A string is a one-dimensional array of characters. An array of strings is a collection of strings and these strings are a combination of characters stored in an array. Operations like sorting, deletion, searching, and modification of values can be performed easily.
In this article, we will learn about an array of string in C. We will look at its initialization and different ways to create an array of strings. Later in the blog, we will discuss about the examples and different functions associated with strings.
What is an Array of Strings in C?
In C programming language, a string is a combination of characters that gets terminated with a null character(\0). Thus, an array of strings is an array string. It is a 2-dimensional array of characters. These strings of an array are of character data type.
Syntax
The syntax for array of strings in C is:
char arrayName[size][maxStringLength];
arrayName: name of an array
size: number of strings an array will hold
maxStringLength: maximum length of the string
Functions of strings
There are various functions that can be used to manipulate strings in C: Some of the most used functions are discussed below:
strlen() Function in C
The strlen() function is used to find the length of a string.
Syntax
size_t strlen(const char * str)
It accepts a pointer to a string and returns the length of the string excluding the null character.
strcpy() Function in C
The strcpy() function copies the same string from source to destination.
Syntax
char *strcpy(char *dest, const char *src);
It accepts two pointers and then copies the characters of the source string to the destination string and return the pointer of the destination string.
strcmp() Function in C
The strcmp() function is used to compare two strings.
Syntax
int strcmp(const char * str1, const char* str2)
It returns 0 if both strings are equal and if they are not equal it returns a non-zero value.
strcat() Function in C
It concatenates the source string to the destination string.
Syntax
char *strcat(char *dest, const char *src);
It accepts two pointers and then concatenates the source characters in the destination string. It returns the pointer of the destination string.
strchr() Function in C
To search a given character in the string.
Syntax
char *strchr(const char *str, int ch);
It accepts two arguments and returns the pointer of the first occurrence of the character.
Invalid operations on an array of strings
There are various operations that are considered invalid when performed with an array of strings. Some of them are discussed below:
1. Strings are immutable so you cannot modify or change them. It is because of the reason that strings in the pointer array are stored as constant char arrays and are in a read-only state.
char* str = ["hello"];
str[0] = "hi";
The above code will through an error as you try to access a string that is stored by a pointer character array.
2. You cannot modify the string but you can always change the pointer to point towards the other string.
As you can see, we only initialize 3 values in the array so accessing the 4th value will cause the out-of-bound array error.
When a pointer is not initialized correctly, it often leads to invalid memory access.
When you create an array of characters, it is important to end the string with the null character to mark the end of the string. If not done correctly, functions like strcpy(), strlen() continue to run, causing overflow.
In the above example, the str array did not end with the null terminator. As a result, it will continuously read the str and will show undefined behavior.
3. Freeing up the memory space that is not dynamically allocated. When you try to remove the memory for a string literal, it is considered an invalid operation as you may see an undefined behavior of the code.
char * str[]= { "Aditya"};
free(str);
In the above code when you try to remove the memory allocated to the string literal, it must be an invalid operation.
How Does Array of Strings Work in C?
In c programming, a string is a 1-dimensional array of characters. But an Array of strings is a two-dimensional array of character data types. Each element in this array is a string and each string gets terminated by a null character or null terminator ("\n"). These strings of an array can be accessed using indexing and also be manipulated as any other arrays.
Methods to Create an Array of Strings
To create an array of strings there are two ways.
Using a two-dimensional array
Using pointers
Both of these methods are used to create an array of strings.
1. Using Two-Dimensional Arrays Notation to Declare Array of Strings in C
Two-dimensional arrays are used to create an array of characters. This is one of the applications and uses of 2-dimensional arrays. Initialization of strings is quite easy. In C programming, a string of array is a 2D array of characters. In a string array, every string terminates with a null character. Before defining the length of a string, we also consider a null character to the length of a string.
Syntax
char var_name[m][n] = { list of strings}
Below is the description for each element:
char: data type to store characters
var_name: variable name that stores a list of strings
m: number of string values that an array can hold
n: number of character values that a string can hold
Reference:
char strings_rep [2][7] = { "Hello", "Hi"};
The above representation of an array of string can be also implemented in another way.
Memory Representation of an Array of Strings in a 2-D Array
It is important to understand how memory is allocated to a 2-D array of characters. Below is the memory representation of an array of strings:
char strings_rep [2][7] = { "Hello", "Hi"};
In this, we have 2 rows and 7 columns that represent the memory consumption. The blank boxes still occupy space in the memory. Now since the array is predefined with the values, space consumption occurs and to avoid these and have proper utilization of space we make use of pointers to define the array of strings.
2. Using Pointers to Create an Array of Strings in C
In C programming we can also create an array using pointers. By using this, we can save memory consumption which is the drawback when we create an array using a 2-dimensional array. Unlike 2 dimensional arrays, we will use a single-dimensional array to create an array of strings.
We will create a pointer(*) of the character data type. This pointer will store the base address of the character array means through this base address of the array, we can traverse the complete array. With this, the string is also stored somewhere else in the memory and gets allocated with the desired size and these pointers in the array will point to their base address.
It is crucial to understand that this pointer internally will be a const character array which means you cannot modify/change this.
Syntax
char* array_name[]= {"String1", "String2"......}
Description:
char*: This is the character pointer that will point towards the base address of an array.
array_name: This is the array_name which user will define
Memory Representation of an Array of Strings Using Pointers
Let’s first create an array of strings using a pointer. For example:
Below is array of strings:
Naman
Riya
Aditya
Sakshi
Frequently Asked Questions
What are the common ways to initialize an array of strings in C?
The most and common way to initialize an array of strings in C is to directly assign values to the array or use string literals to assign value to each element.
How does dynamic memory allocation work for an array of strings in C?
Dynamic memory allocation for an array of strings in C is done by using malloc() and calloc() functions of C. These are used to allocate memory for an array of pointers to characters each allocating memory for a string individually.
What are the common errors when working with arrays of strings in C?
The most common mistakes encountered with this array of strings in C is out-of-bounds access, insufficient allocation of space for the string, a missing null terminator (\0), and the poor management of deallocation which may even result in a memory leak.
How string is created in the C programming language?
Other programming languages have a string as a data type however in C programming language string data type is not supported. So to create a string in C we use two different methods. We can use a 2-dimensional array or by using pointers.
Conclusion
In C programming language, a string is an arrangement of characters that gets terminated with a null character(\\0). Thus, an array of strings is an array string. It is a 2-dimensional array of characters. These strings of an array are of character data type. There are multiple ways to initializer an array of strings in C. Each having its usage and time.