- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Array of Strings in C
In C programming language, a string is an array of character sequences terminated by NULL, it is a one-dimensional array of characters. And, the array of strings is an array of strings (character array).
What is an Array of Strings in C?
Thus, an array of strings can be defined as –
An array of strings is a two-dimensional array of character-type arrays where each character array (string) is null-terminated.
To declare a string, we use the statement −
char string[] = {'H', 'e', 'l', 'l', 'o', '\0'}; Or char string = "Hello";
Declare and Initialize an Array of Strings
To declare an array of strings, you need to declare a two-dimensional array of character types, where the first subscript is the total number of strings and the second subscript is the maximum size of each string.
To initialize an array of strings, you need to provide the multiple strings inside the double quotes separated by the commas.
Syntax
To construct an array of strings, the following syntax is used −
char strings [no_of_strings] [max_size_of_each_string];
Example
Let us declare and initialize an array of strings to store the names of 10 computer languages, each with the maximum length of 15 characters.
char langs [10][15] = { "PYTHON", "JAVASCRIPT", "PHP", "NODE JS", "HTML", "KOTLIN", "C++", "REACT JS", "RUST", "VBSCRIPT" };
Printing An Array of Strings
A string can be printed using the printf() function with %s format specifier. To print each string of an array of strings, you can use the for loop till the number of strings.
Example
In the following example, we are declaring, initializing, and printing an array of string −
#include <stdio.h> int main (){ char langs [10][15] = { "PYTHON", "JAVASCRIPT", "PHP", "NODE JS", "HTML", "KOTLIN", "C++", "REACT JS", "RUST", "VBSCRIPT" }; for (int i = 0; i < 10; i++){ printf("%s\n", langs[i]); } return 0; }
Output
When you run this code, it will produce the following output −
PYTHON JAVASCRIPT PHP NODE JS HTML KOTLIN C++ REACT JS RUST VBSCRIPT
Note: The size of each string is not equal to the row size in the declaration of the array. The "\0" symbol signals the termination of the string and the remaining cells in the row are empty. Thus, a substantial part of the memory allocated to the array is unused and thus wasted.
How an Array of Strings is Stored in Memory?
We know that each char type occupies 1 byte in the memory. Hence, this array will be allocated a block of 150 bytes. Although this block is contagious memory locations, each group of 15 bytes constitutes a row.
Assuming that the array is located at the memory address 1000, the logical layout of this array can be shown as in the following figure −
An Array of Strings with Pointers
To use the memory more efficiently, we can use the pointers. Instead of a 2D char array, we declare a 1D array of "char *" type.
char *langs[10] = { "PYTHON", "JAVASCRIPT", "PHP", "NODE JS", "HTML", "KOTLIN", "C++", "REACT JS", "RUST", "VBSCRIPT" };
In the 2D array of characters, the strings occupied 150 bytes. As against this, in an array of pointers, the strings occupy far less number of bytes, as each string is randomly allocated memory as shown below −
Note: Here, lang[ ] is an array of pointers of individual strings.
Example
We can use a for loop as follows to print the array of strings −
#include <stdio.h> int main(){ char *langs[10] = { "PYTHON", "JAVASCRIPT", "PHP", "NODE JS", "HTML", "KOTLIN", "C++", "REACT JS", "RUST", "VBSCRIPT" }; for (int i = 0; i < 10; i++) printf("%s\n", langs[i]); return 0; }
Output
When you run this code, it will produce the following output −
PYTHON JAVASCRIPT PHP NODE JS HTML KOTLIN C++ REACT JS RUST VBSCRIPT
Here, langs is a pointer to an array of 10 strings. Therefore, if langs[0] points to the address 5000, then "langs + 1" will point to the address 5004 which stores the pointer to the second string.
Hence, we can also use the following variation of the loop to print the array of strings −
for(int i = 0; i < 10; i++){ printf("%s\n", *(langs + i)); }
When strings are stored in array, there are a lot use cases. Let study some of the use cases.
Find the String with the Largest Length
In the following example, we store the length of first string and its position (which is "0") in the variables "l" and "p" respectively. Inside the for loop, we update these variables whenever a string of larger length is found.
Example
Take a look at the following example −
#include <stdio.h> #include <string.h> int main (){ char langs [10][15] = { "PYTHON", "JAVASCRIPT", "PHP", "NODE JS", "HTML", "KOTLIN", "C++", "REACT JS", "RUST", "VBSCRIPT" }; int l = strlen(langs[0]); int p = 0; for (int i = 0; i < 10; i++){ if (strlen(langs[i]) >= l){ l = strlen(langs[i]); p = i; } } printf("Language with the longest name: %s Length: %d", langs[p], l); return 0; }
Output
When you run this code, it will produce the following output −
Language with longest name: JAVASCRIPT Length: 10
Sort a String Array in Ascending Order
We need to use the strcmp() function to compare two strings. If the value of comparison of strings is greater than 0, it means the first argument string appears later than the second in alphabetical order. We then swap these two strings using the strcmp() function.
Example
Take a look at the following example −
#include <stdio.h> #include <string.h> int main (){ char langs [10][15] = { "PYTHON", "JAVASCRIPT", "PHP", "NODE JS", "HTML", "KOTLIN", "C++", "REACT JS", "RUST", "VBSCRIPT" }; int i, j; char temp[15]; for (i = 0; i < 9; i++){ for (j = i + 1; j < 10; j++){ if (strcmp(langs[i], langs[j]) > 0){ strcpy(temp, langs[i]); strcpy(langs[i], langs[j]); strcpy(langs[j], temp); } } } for (i = 0; i < 10; i++){ printf("%s\n", langs[i]); } return 0; }
Output
When you run this code, it will produce the following output −
C++ HTML JAVASCRIPT KOTLIN NODE JS PHP PYTHON REACT JS RUST VBSCRIPT
In this chapter, we explained how you can declare an array of strings and how you can manipulate it with the help of string functions.