C++ में Array Size छोड़ना
In this page:
data_type array_name[] = {value1, value2, value3}; // size is inferred
Initialization पर Size Omit करना
एक array declare करते समय और साथ ही इसकी initial values देते समय, square brackets के अंदर size को खाली छोड़ा जा सकता है, और compiler array का size decide करने के लिए initializer values count करता है।
उदाहरण: Omitting the Size at Initialization
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
int arr[] = {1, 2, 3, 4, 5};
// Print to the console with cout
std::cout << "Size inferred as 5 elements" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
यह क्यों काम करता है
Compiler किसी array का size सिर्फ तभी infer कर सकता है जब declaration के moment पर initial values की एक complete list दी गई हो, क्योंकि यह exactly count करता है कि braces के अंदर कितनी values हैं।
उदाहरण: Why This Works
#include <iostream>
int main() {
int arr[] = {10, 20, 30}; // compiler counts 3 initializers
std::cout << sizeof(arr) / sizeof(arr[0]) << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
जब Size Omit नहीं हो सकता
Size को omit नहीं किया जा सकता अगर array को बिल्कुल भी बिना किसी initializer values के declare किया जाए, क्योंकि compiler के पास तब count करने के लिए कुछ नहीं होगा और memory reserve करने के लिए एक explicit size चाहिए।
उदाहरण: When Size Cannot Be Omitted
#include <iostream>
int main() {
int arr[5]; // no initializer -- size must be given explicitly
arr[0] = 1;
std::cout << arr[0] << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
String Arrays के साथ Omitted Size
C-style strings के एक array को initialize करते समय size omit करना भी naturally काम करता है, compiler को count करने देते हुए कि कितनी string literals दी गईं।
उदाहरण: Omitted Size with String Arrays
// Include std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
// Program execution starts in main()
int main() {
std::string names[] = {"Alice", "Bob", "Carol"};
// Loop: repeat while the condition holds
for (int i = 0; i < 3; i++) {
// Print to the console with cout
std::cout << names[i] << " ";
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Omitted Size को Partial Fill के साथ Combine करना
Intended से कम initializer values देना फिर भी सिर्फ actually list की गई values से match करता एक size infer करता है, इसलिए एक partially intended array को omission पर depend करने की बजाय इसका size explicitly state करना चाहिए।
उदाहरण: Combining Omitted Size with a Partial Fill
#include <iostream>
int main() {
int arr[] = {1, 2}; // size inferred as 2, not larger
std::cout << sizeof(arr) / sizeof(arr[0]) << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- size और initializer दोनों omit करना, जैसे
int a[];, जो एक error है। - यह मान लेना कि compiler एक ऐसा size चुनता है जो आपने नहीं दिया, जब यह सिर्फ initializer list से count होता है।
- बाद में ऐसे array को बढ़ाने की कोशिश करना, जब इसका size fixed है।
Chapter Quiz — Complete all 15 topics to unlock
0/15 topics done
Complete these topics first:
- C++ Arrays Introduction
- C++ Looping Through Arrays
- C++ Omitting Array Size
- C++ Array Size
- C++ Multi-dimensional Arrays
- C++ Arrays & Functions
- C++ Strings (C-style)
- C++ std::string
- C++ String Concatenation
- C++ Converting Strings and Numbers
- C++ String Length
- C++ Accessing String Characters
- C++ using namespace std
- C++ String Methods
- C++ Array of Strings