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

C में Strings का Array

Strings का एक array words की एक list है, कई labeled jars रखने वाले एक shelf जैसा। आप हर word को इसके number से पहुंचते हैं।
Syntax
c
char array_name[count][max_length];

char array_name[count][max_length] = {"text1", "text2"};

Strings का एक Array Declare करना

char names[5][20] जैसी एक declaration हर एक 19 visible characters (plus null terminator) तक की 5 strings के लिए जगह reserve करती है -- यह rigid है लेकिन simple है, char pointers के एक array के उलट जो हर string को अपना independently-sized buffer देता है।

उदाहरण: 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;
}

String Arrays Initialize करना

initializer list में हर string literal automatically unused bytes से padded हो जाता है अगर यह आपके declared row width से छोटी हो, थोड़ी memory waste करते हुए लेकिन हर row को predictable indexing के लिए same fixed size रखते हुए।

उदाहरण: 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;
}

Array के अंदर Strings Access करना

names[2] आपको array में तीसरी string की शुरुआत का एक pointer देता है, जिसे आप सीधे printf("%s", names[2]) या strlen(names[2]) जैसे functions को pass कर सकते हैं, exactly किसी दूसरी string की तरह।

उदाहरण: 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;
}

Array में Strings Modify करना

names[2] = "NewValue" try करना एक fixed 2D char array के लिए एक compile error है क्योंकि आप एक array row को एक pointer की तरह reassign नहीं कर सकते -- strcpy(names[2], "NewValue") एक row का content in place replace करने का सही तरीका है।

उदाहरण: 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;
}

String Arrays के जरिए Loop करना

0 से row count तक एक simple for loop आपको हर नाम print या process करने देता है; बस careful रहें कि आप loop के अंदर जो भी function call करें वह fixed row width से ज़्यादा characters न लिखे।

उदाहरण: 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. #}
आम गलतियां
  1. row size को सबसे लंबी string के लिए काफी बड़ा न बनाना।
  2. एक row को एक string = से assign करना, जो declaration के बाद allowed नहीं है; strcpy इस्तेमाल करें।
  3. strings की संख्या पाने के लिए sizeof(arr) इस्तेमाल करना।
चैप्टर सारांश
  • Arrays same type की कई values store करते हैं, और आप इनमें loop कर सकते हैं, इनका size ले सकते हैं, और इन्हें functions को pass कर सकते हैं।
  • Multi-dimensional arrays data को rows और columns में organize करते हैं।
  • Strings character arrays हैं जिन्हें string functions से handle किया जाता है, और strings का एक array इनमें से कई रखता है।
🔒

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.