The Wayback Machine - https://web.archive.org/web/20240627093152/https://www.geeksforgeeks.org/array-of-strings-in-c/
Open In App

Array of Strings in C

Last Updated : 07 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In C programming String is a 1-D array of characters and is defined as an array of characters. But an array of strings in C is a two-dimensional array of character types. Each String is terminated with a null character (\0). It is an application of a 2d array.

Syntax:

char variable_name[r] = {list of string};

Here,

  • var_name is the name of the variable in C.
  • r is the maximum number of string values that can be stored in a string array.
  • c is the maximum number of character values that can be stored in each string array.

Example: 

C




// C Program to print Array
// of strings
#include <stdio.h>
 
// Driver code
int main()
{
  char arr[3][10] = {"Geek",
                     "Geeks", "Geekfor"};
  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:
Geek
Geeks
Geekfor

Below is the Representation of the above program 

Memory Representation of Array of Strings

 

We have 3 rows and 10 columns specified in our Array of String but because of prespecifying, the size of the array of strings the space consumption is high. So, to avoid high space consumption in our program we can use an Array of Pointers in C.

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"};

Here, 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].

Array of Pointers of Strings

In C we can use an Array of pointers. Instead of having a 2-Dimensional character array, we can have a single-dimensional array of Pointers. Here pointer to the first character of the string literal is stored.

Syntax:

char *arr[] = { "Geek", "Geeks", "Geekfor" };
Array of Pointers of Strings

 

Below is the C program to print an array of pointers:

C




// C Program to print Array
// of Pointers
#include <stdio.h>
 
// Driver code
int main()
{
  char *arr[] = {"Geek", "Geeks", "Geekfor"};
  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:
Geek
Geeks
Geekfor


Previous Article
Next Article

Similar Reads

Array of Pointers to Strings in C
In C, arrays are data structures that store data in contiguous memory locations. Pointers are variables that store the address of data variables. We can use an array of pointers to store the addresses of multiple elements. In this article, we will learn how to create an array of pointers to strings in C. Array of Pointers to Strings in CAn array of
2 min read
How to Create a Dynamic Array of Strings in C?
In C, dynamic arrays are essential for handling data structures whose size changes dynamically during the program's runtime. Strings are arrays of characters terminated by the null character '\0'. A dynamic array of strings will ensure to change it's size dynamically during the runtime of the program as per the user's needs. In this article, we wil
3 min read
What’s difference between “array” and “&amp;array” for “int array[5]” ?
If someone has defined an array such as “int array[5]”, what’s the meaning of “array” or “&amp;array”? Are they both same or are they different? You might be tempted to think that they both would point to the very first element of the array i.e. they both will have same address. Let us find out! To check this, the very first thing that comes to min
4 min read
C Program to Add 2 Binary Strings
Given two Binary Strings, we have to return their sum in binary form. Approach: We will start from the last of both strings and add it according to binary addition, if we get any carry we will add it to the next digit. Input: 11 + 11 Output: 110 C/C++ Code // C Program to Add 2 Binary Strings // and Print their Binary Sum #include &lt;stdio.h&gt; #
8 min read
C Program to Split a String into a Number of Sub-Strings
Splitting a string by some delimiter is a very common task. For example, we have a comma-separated list of items from a file and we want individual items in an array. Almost all programming languages, provide a function split a string by some delimiter.  In C:  // Splits str[] according to given delimiters. // and returns next token. It needs to be
1 min read
Concatenating Two Strings in C
Given two strings str1 and str2, our task is to concatenate these two strings. There are multiple ways to concatenate two strings in C language: Without Using strcat() functionUsing standard methodUsing functionUsing recursionUsing strcat() function1. Concatenating Two strings without using the strcat() functionA. Using Standard MethodInput: str1 =
6 min read
Check if two strings are same or not without using library functions
Given two strings S1 and S2, the task is to check whether they are the same or not without using string library functions. Examples: Input: S1 = ”GeeksForGeeks”, S2 = ”GeeksForGeeks”Output: TrueExplanation:S1 and S2 are the same strings Input: S1 = ”GeeksForGeeks”, S2 = ”GeeksforGeeks”Output: False Approach: Follow the steps below to solve the prob
5 min read
C Program to Compare Two Strings Lexicographically
Here, we will build a C Program to compare two strings lexicographically. Given 2 strings s1 and s2, of lowercase English alphabets, denote the lexicographically greater string. Lexicographical order is the order in which words appear in the order of a dictionary. Input: s1 = "geeks", s2 = "geeksforgeeks Output: String 2 is lexicographically greate
2 min read
Program to check if two strings are same or not
Given two strings, the task is to check if these two strings are identical(same) or not. Examples: Input: string1 = "GeeksforGeeks", string2 = "GeeksforGeeks" Output: Yes Input: string1 = "Geeks for Geeks", string2 = "Geeks for Geeks" Output: Yes Input: string1 = "GeeksforGeeks", string2 = "Geeks" Output: No Input: string1 = "Geeks for Geeks", stri
7 min read
Storage for Strings in C
In C, a string can be referred to either using a character pointer or as a character array. Strings as character arrays C/C++ Code char str[4] = &quot;GfG&quot;; /*One extra for string terminator*/ /* OR */ char str[4] = {‘G’, ‘f’, ‘G’, '&#92;&#48;'}; /* '&#92;&#48;' is string terminator */ When strings are declared as character arrays, they are st
5 min read
Article Tags :
three90RightbarBannerImg