We read your photo as:

How do you declare and initialize an array of strings in C?

Computer Science

ScanSolve AI Answer

Step 1: Array Declaration and Initialization in C
To declare and initialize an array of strings in C, you need to define the maximum number of names and the maximum length for each name.

#include <stdio.h>
#include <string.h> // Required for strcpy

#define MAX_NAMES 5       // Maximum number of names
#define MAX_NAME_LEN 20   // Maximum length for each name (including null terminator)

int main() {
    // Declare an array of strings
    char names[MAX_NAMES][MAX_NAME_LEN];

    // Initialize the array with sample names
    strcpy(names[0], "Alice");
    strcpy(names[1], "Bob");
    strcpy(names[2], "Charlie");
    strcpy(names[3], "David");
    strcpy(names[4], "Eve");

    // Optional: Print the initialized array to verify
    printf("Initialized Names:\n");
    for (int i = 0; i < MAX_NAMES; i++) {
        printf("%s\n", names[i]);
    }

    return 0;
}

The array names is declared as a 2D character array, where MAX_NAMES is the number of rows (strings) and MAX_NAME_LEN is the number of columns (characters per string). Each string is then initialized using the strcpy function.

The final answer for the array declaration and initialization is:

#define MAX_NAMES 5
#define MAX_NAME_LEN 20
char names[MAX_NAMES][MAX_NAME_LEN] = {"Alice", "Bob", "Charlie", "David", "Eve"};
Was this helpful?

Still stuck on this one?

Ask a follow-up, and the answer starts from this question.

Got a different question?Ask your own question