C में Strings का Array
In this page:
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
// 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
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
// 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
Array के अंदर Strings Access करना
names[2] आपको array में तीसरी string की शुरुआत का एक pointer देता है, जिसे आप सीधे printf("%s", names[2]) या strlen(names[2]) जैसे functions को pass कर सकते हैं, exactly किसी दूसरी string की तरह।
उदाहरण: 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
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
// 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
String Arrays के जरिए Loop करना
0 से row count तक एक simple for loop आपको हर नाम print या process करने देता है; बस careful रहें कि आप loop के अंदर जो भी function call करें वह fixed row width से ज़्यादा characters न लिखे।
उदाहरण: 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
- row size को सबसे लंबी string के लिए काफी बड़ा न बनाना।
- एक row को एक string
=से assign करना, जो declaration के बाद allowed नहीं है;strcpyइस्तेमाल करें। - 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: