- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- 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 - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Array of Strings
![]() Share with a Friend |
C Programming - C Array of Strings
C Array of Strings
An array of strings in C is essentially a two-dimensional array where each row is a string (an array of characters). It is a common way to store multiple strings in a single variable, making it easier to manage and manipulate a group of strings.
- Declaration and Initialization
Declaration:
To declare an array of strings:
C
char array_of_strings[row][column];
- row: Number of strings.
- column: Maximum length of each string (including the null character).
Initialization:
(a) Static Initialization:
C
char names[3][10] = {"Alice", "Bob", "Charlie"};
(b) Dynamic Initialization:
C
char names[3][10];
strcpy(names[0], "Alice");
strcpy(names[1], "Bob");
strcpy(names[2], "Charlie");
- Accessing and Modifying Strings
Each string can be accessed using its index:
C
printf("%s\n", names[0]); // Prints "Alice"
names[1][0] = 'R'; // Modifies "Bob" to "Rob"
- Input and Output
To take input for an array of strings:
C
#include <stdio.h>
int main() {
char cities[3][20];
printf("Enter names of 3 cities:\n");
for (int i = 0; i < 3; i++) {
scanf("%s", cities[i]); // Input for each string
}
printf("The cities are:\n");
for (int i = 0; i < 3; i++) {
printf("%s\n", cities[i]);
}
return 0;
}
Output:
Enter names of 3 cities:
London
Paris
Tokyo
The cities are:
London
Paris
Tokyo
- Passing Array of Strings to a Function
An array of strings can be passed to a function just like any other array.
Example:
C
#include <stdio.h>
void printStrings(char strings[][20], int n) {
for (int i = 0; i < n; i++) {
printf("%s\n", strings[i]);
}
}
int main() {
char fruits[3][20] = {"Apple", "Banana", "Cherry"};
printStrings(fruits, 3);
return 0;
}
- Using Pointers for Array of Strings
Instead of a two-dimensional array, an array of pointers to strings can be used:
C
char *names[] = {"Alice", "Bob", "Charlie"};
Advantages:
- Saves memory since strings of different lengths don't occupy the same fixed space.
- Easier to manage dynamically allocated strings.
Example:
C
#include <stdio.h>
int main() {
char *languages[] = {"C", "Python", "Java"};
for (int i = 0; i < 3; i++) {
printf("%s\n", languages[i]);
}
return 0;
}
- Example Program: Sorting an Array of Strings
C
#include <stdio.h>
#include <string.h>
int main() {
char strings[5][20] = {"Banana", "Apple", "Cherry", "Mango", "Grape"};
char temp[20];
// Sorting strings
for (int i = 0; i < 5; i++) {
for (int j = i + 1; j < 5; j++) {
if (strcmp(strings[i], strings[j]) > 0) { // Compare strings
strcpy(temp, strings[i]);
strcpy(strings[i], strings[j]);
strcpy(strings[j], temp);
}
}
}
printf("Sorted Strings:\n");
for (int i = 0; i < 5; i++) {
printf("%s\n", strings[i]);
}
return 0;
}
Output:
Sorted Strings:
Apple
Banana
Cherry
Grape
Mango
- Advantages and Limitations
Advantages:
- Simplifies handling multiple strings.
- Allows grouping of related strings (e.g., names, items, etc.).
Limitations:
- Fixed size if declared as a 2D array.
- Managing dynamic memory requires extra care with pointers to avoid memory leaks.
- Summary
An array of strings is a fundamental concept in C programming that provides an efficient way to handle multiple strings together. Depending on the use case, you can choose between a fixed-size 2D array or a dynamically allocated array of pointers to strings.
