← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 3 of 15

C++ में Array Size छोड़ना

जब एक array को declaration पर values से initialize किया जाता है, square brackets में size को पूरी तरह omit किया जा सकता है, और compiler इसे initializer values की संख्या से automatically infer करता है।
Syntax
cpp
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

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

यह क्यों काम करता है

Compiler किसी array का size सिर्फ तभी infer कर सकता है जब declaration के moment पर initial values की एक complete list दी गई हो, क्योंकि यह exactly count करता है कि braces के अंदर कितनी values हैं।

उदाहरण: Why This Works

cpp
#include <iostream>

int main() {
	int arr[] = {10, 20, 30}; // compiler counts 3 initializers
	std::cout << sizeof(arr) / sizeof(arr[0]) << std::endl;
	return 0;
}

जब Size Omit नहीं हो सकता

Size को omit नहीं किया जा सकता अगर array को बिल्कुल भी बिना किसी initializer values के declare किया जाए, क्योंकि compiler के पास तब count करने के लिए कुछ नहीं होगा और memory reserve करने के लिए एक explicit size चाहिए।

उदाहरण: When Size Cannot Be Omitted

cpp
#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;
}

String Arrays के साथ Omitted Size

C-style strings के एक array को initialize करते समय size omit करना भी naturally काम करता है, compiler को count करने देते हुए कि कितनी string literals दी गईं।

उदाहरण: Omitted Size with String Arrays

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

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

cpp
#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;
}
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. size और initializer दोनों omit करना, जैसे int a[];, जो एक error है।
  2. यह मान लेना कि compiler एक ऐसा size चुनता है जो आपने नहीं दिया, जब यह सिर्फ initializer list से count होता है।
  3. बाद में ऐसे array को बढ़ाने की कोशिश करना, जब इसका size fixed है।

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.