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

C++ का Array Size

एक array का size declaration पर fixed होता है, और sizeof को एक element के size से divide करना यह compute करने का standard C++ तरीका है कि इसमें कितने elements हैं, हालांकि std::vector इससे पूरी तरह बचता है।
Syntax
cpp
data_type array_name[size] = {value1, value2};

int length = sizeof(array_name) / sizeof(array_name[0]);

Declaration पर Fixed Size

एक C-style array का size उस moment fixed हो जाता है जब यह declare होता है, square brackets के अंदर एक number की तरह specify किया गया, और std::vector के उलट, यह इसके बाद बढ़ या सिकुड़ नहीं सकता।

उदाहरण: Fixed Size at Declaration

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[5] = {1, 2, 3, 4, 5};
	// Print to the console with cout
	std::cout << "Fixed size: 5" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

sizeof से Array का Size ढूंढना

एक array पर apply किया गया sizeof operator इसके occupy किए गए total bytes return करता है, और इसे sizeof(arr[0]) इस्तेमाल करके एक single element के size से divide करना यह compute करने का standard तरीका है कि इसमें कितने elements हैं।

उदाहरण: Finding an Array's Size with sizeof

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[] = {1, 2, 3, 4, 5};
	// Declare count and set it to sizeof(arr) / sizeof(arr[0])
	int count = sizeof(arr) / sizeof(arr[0]);
	// Print to the console with cout
	std::cout << count << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Functions को Pass किए गए Arrays का Size

जब एक array को एक function parameter की तरह pass किया जाता है, यह एक pointer में decay हो जाता है, इसलिए उस function के अंदर sizeof original array की बजाय pointer का size report करता है, element count को अलग से pass करना ज़रूरी बनाते हुए।

उदाहरण: Size of Arrays Passed to Functions

cpp
#include <iostream>

void checkSize(int arr[]) {
	std::cout << sizeof(arr) << std::endl; // reports pointer size, not array size
}

int main() {
	int arr[] = {1, 2, 3, 4, 5};
	std::cout << sizeof(arr) << std::endl; // full array size
	checkSize(arr);
	return 0;
}

Modern Array Sizing के लिए std::size()

C++17 ने free function std::size() introduce किया, जो sizeof division trick के बिना सीधे एक array का element count return करता है, C-style arrays और STL containers दोनों पर काम करते हुए।

उदाहरण: std::size() for Modern Array Sizing

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 << std::size(arr) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

एक Sizeable Alternative की तरह std::vector

एक C-style array के उलट, एक std::vector internally अपना size track करता है और runtime पर बढ़ या सिकुड़ सकता है, और इसका .size() method हमेशा सीधे current element count return करता है।

उदाहरण: std::vector as a Sizeable Alternative

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>

// Program execution starts in main()
int main() {
	// Create a vector named vec (a resizable array)
	std::vector<int> vec = {1, 2, 3};
	vec.push_back(4);
	// Print to the console with cout
	std::cout << vec.size() << std::endl;
	// 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. एक function के अंदर sizeof(arr) इस्तेमाल करना जहाँ array pointer में decay हो चुका है, length की बजाय 8 देते हुए।
  2. sizeof(arr) को गलत element type से divide करना।
  3. int a[n]; जैसा एक non-constant size इस्तेमाल करना और इसे standard C++ मानना (यह एक compiler extension है)।

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.