How to define a string array in the C language?

In C language, you can use character arrays to define arrays of strings. The specific syntax is as follows:

char strArray[N][M];

In this case, N represents the size of the array of strings and M represents the maximum length of each string (including the ‘ \0’ character at the end of the string).

For example, to define a string array with 3 strings, each having a length of 10, you can use the following statement:

char strArray[3][10];

This defines an array of strings with a size of 3, where each string has a maximum length of 10. The strings in the array can be accessed and modified using indexes.

strcpy(strArray[0], "Hello");
printf("%s\n", strArray[0]);  // 输出: Hello

strcpy(strArray[1], "World");
printf("%s\n", strArray[1]);  // 输出: World

It is important to note that the length of a character array is fixed and will not automatically adjust in size. Exceeding the array’s length may result in memory overflow or overwriting of other data. When using a character array, make sure to not access or modify positions beyond the array’s boundaries.

bannerAds