← Back to C Course | Chapter 6: Arrays & Strings | Lesson 8 of 8

C Array of Strings

An array of strings is a list of words, like a shelf holding several labeled jars. You reach each word by its number.
Syntax
c
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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}

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

c
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Not making the row size large enough for the longest string.
  2. Assigning a string to a row with =, which is not allowed after declaration; use strcpy.
  3. Using sizeof(arr) to get the number of strings.
Chapter Summary
  • 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:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.