C Arrays of Strings (2D Character Arrays)

In C, you can work with arrays of strings using 2D character arrays. These arrays are essentially arrays of strings, where each element in the array is a string represented as a character array. Here's an example of how to declare, initialize, and manipulate a 2D character array (array of strings):

#include <stdio.h>
#include <string.h>

int main() {
// Declare and initialize a 2D character array
char strings[3][20] = {
	"Hello,",
	"World!",
	"C Programming"
};

// Print the array of strings
for (int i = 0; i < 3; i++) {
	printf("String %d: %s\n", i + 1, strings[i]);
}

// Modify an element in the array
strcpy(strings[1], "Goodbye!");

// Print the modified array of strings
printf("\nModified Array of Strings:\n");
for (int i = 0; i < 3; i++) {
	printf("String %d: %s\n", i + 1, strings[i]);
}

return 0;
}

In this example:

  1. We declare a 2D character array called strings with 3 rows and a maximum of 20 characters per string.
  2. We initialize the array with three strings.
  3. We use a for loop to print each string in the array.
  4. We modify the second string in the array using strcpy to change "World!" to "Goodbye!"
  5. We print the modified array of strings.

This code demonstrates the basic usage of a 2D character array in C for storing and manipulating multiple strings. You can add more strings or change the maximum length as needed for your specific use case.