C Array of Strings
In this page:
char array_name[count][max_length];
char array_name[count][max_length] = {"text1", "text2"};
Declaring an Array of Strings
A declaration like char names[5][20] reserves room for 5 strings of up to 19 visible characters each (plus the null terminator) -- it's rigid but simple, unlike an array of char pointers which lets each string have its own independently-sized buffer.
Example: Declaring an Array of Strings
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
char names[5][20];
// Print formatted text to the screen
printf("%zu", sizeof(names));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Initializing String Arrays
Each string literal in the initializer list is automatically padded with unused bytes if it's shorter than the row width you declared, wasting a little memory but keeping every row the same fixed size for predictable indexing.
Example: Initializing String Arrays
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
char names[3][20] = {"Ann", "Bob", "Cat"};
// Print formatted text to the screen
printf("%s", names[1]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Accessing Strings inside the Array
names[2] gives you a pointer to the start of the third string in the array, which you can pass directly to functions like printf("%s", names[2]) or strlen(names[2]) exactly as you would any other string.
Example: Accessing Strings inside the Array
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
char names[3][20] = {"Ann", "Bob", "Cat"};
// Print formatted text to the screen
printf("%s", names[2]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Modifying Strings in the Array
Attempting names[2] = "NewValue" is a compile error for a fixed 2D char array because you cannot reassign an array row as if it were a pointer -- strcpy(names[2], "NewValue") is the correct way to replace a row's contents in place.
Example: Modifying Strings in the Array
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include string functions (strlen, strcpy, ...)
#include <string.h>
// Program execution starts in main()
int main() {
char names[3][20] = {"Ann", "Bob", "Cat"};
strcpy(names[2], "NewValue");
// Print formatted text to the screen
printf("%s", names[2]);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Looping through String Arrays
A simple for loop from 0 to the row count lets you print or process each name; just be careful that whatever function you call inside the loop doesn't write more characters than the fixed row width can hold.
Example: Looping through String Arrays
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
char names[3][20] = {"Ann", "Bob", "Cat"};
// Loop: repeat while the condition holds
for (int i = 0; i < 3; i++) {
// Print formatted text to the screen
printf("%s ", names[i]);
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Not making the row size large enough for the longest string.
- Assigning a string to a row with
=, which is not allowed after declaration; usestrcpy. - Using
sizeof(arr)to get the number of strings.
- Arrays store several values of the same type, and you can loop through them, size them, and pass them to functions.
- Multi-dimensional arrays organize data in rows and columns.
- Strings are character arrays handled with string functions, and an array of strings holds several of them.
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: