Table of Contents
Array of Strings in C
In C programming, a string is indeed a one-dimensional array of characters and is typically defined as an array of characters. Each string is terminated with a null character, represented as '\0'
, which marks the end of the string.
An array of strings in C, often referred to as a “two-dimensional array of character types,” is essentially an array of arrays of characters. Each element in the first dimension is a string (which is an array of characters), and you can have multiple such strings in the second dimension. This creates a tabular structure, making it a 2D array. It’s commonly used to store and manage multiple strings, making it an application of a 2D array.
Syntax:
char variable_name[r] = {list of string};
In C, you can use variables with the following names and meanings:
var_name
: This variable represents the name of a variable in your C program. It’s a placeholder for the actual variable name you want to use in your code. For example, if you want to create an integer variable, you can useint var_name;
and then replacevar_name
with your desired variable name, likeint age;
.r
: This variable represents the maximum number of string values that can be stored in a string array. If you have an array of strings and want to specify the maximum number of strings it can hold, you can useint r;
and assign the desired value tor
.c
: This variable represents the maximum number of character values that can be stored in each string array. In the context of a 2D character array representing an array of strings,c
would typically refer to the maximum length of each individual string. You can useint c;
and assign the desired value toc
to set the maximum length for each string in the array.
Example
// C Program to print Array
// of strings
#include <stdio.h>
// Driver code
int main()
{
char arr[3][10] = {"Skill",
"Skills", "Skillver"};
printf("String array Elements are:\n");
for (int i = 0; i < 3; i++)
{
printf("%s\n", arr[i]);
}
return 0;
}
Output
String array Elements are:
Skill
Skills
Skillver
Invalid Operations in Arrays of Strings
In C, you can directly change or assign values to an array of strings. An array of strings is essentially a 2D array of characters, where each row (or element in the first dimension) represents a string (a sequence of characters terminated by a null character ‘\0’).
Example
char arr[3][10] = {"Skill", "Skills", "Skillver"};
We can’t directly change or put a value of array of string in C
The value of arr[0]
in the array char arr[3][10] = {"Skill", "Skills", "Skillver"};
is the string “Skill.” So, arr[0]
represents the string “Skill,” which is a sequence of characters ending with the null terminator ‘\0’.
To change values we can use strcpy() function in C
strcpy(arr[0],"GFG"); // This will copy the value to the arr[0].
Array of Pointers of Strings
In C, you can use an array of pointers to represent an array of strings. This is often referred to as an array of pointers to characters, and it provides a more flexible way to manage strings compared to a 2D character array.
Syntax
char *arr[] = { "Skill", "Skills", "Skillver" };
C Program to print an array of pointers
// C Program to print Array
// of Pointers
#include <stdio.h>
// Driver code
int main()
{
char *arr[] = {"Skill", "Skills", "Skillver"};
printf("String array Elements are:\n");
for (int i = 0; i < 3; i++)
{
printf("%s\n", arr[i]);
}
return 0;
}
Output
String array Elements are:
Skill
Skills
Skillver
FAQ- Array of Strings in C
Q1. How to get array of strings in C?
Ans. In C, an array of strings is typically represented as a one-dimensional array of strings, where each element of the array is a pointer to a string (which is essentially a one-dimensional array of characters). This is often referred to as an array of pointers to characters.
Q2. How do you write an array of strings?
Ans. String[] strArray = new String[3]; strArray[0] = “one”; strArray[1] = “two”; strArray[2] = “three”; Here the String Array is declared first. Then in the next line, the individual elements are assigned values
Q3. What is the syntax of string in C?
Ans. char string_name[size]; is the syntax of string in C
Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together