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

C++ Arrays का परिचय

एक array boxes की एक row है जो same तरह की value रखते हैं, हर एक का number 0 से शुरू होता है। यह number से खोले जाने वाले lockers की एक row जैसा है।
Syntax
cpp
data_type array_name[size];

data_type array_name[size] = {value1, value2, value3};

array_name[index];  // access an element

Array क्या है?

एक array same data type की कई values को एक variable नाम के तहत group करता है, memory में contiguously साथ store किया गया, यही वजह है कि किसी भी element को इसकी position से access करना तेज़ और predictable दोनों है।

यह contiguous layout array और दूसरे collection types के बीच key difference है जो अपने elements को memory में बिखेर सकते हैं।

उदाहरण: What is an Array?

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

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

Arrays Declare और Initialize करना

आप curly braces इस्तेमाल करके एक single statement में एक array declare और भर सकते हैं, जैसे int scores[] = {90, 85, 78};, और अगर आप brackets में size छोड़ दें, compiler इसे decide करने के लिए automatically initializer values count करता है।

अपनी initializer list से बड़ी एक size explicitly specify करना बाकी elements को zero-initialized छोड़ता है।

उदाहरण: Declaring and Initializing Arrays

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

// Program execution starts in main()
int main() {
	int scores[] = {90, 85, 78};
	// Print to the console with cout
	std::cout << scores[0] << " " << scores[1] << " " << scores[2] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Array Elements Access करना

Array indexing zero-based है, मतलब पहला element index 0 पर बैठता है और आखिरी size - 1 पर, खुद size पर नहीं — index size access करने की कोशिश array की actual memory से आगे पढ़ती है और एक helpful error की बजाय undefined behavior produce करती है।

यह off-by-one boundary array-heavy code में bugs के सबसे common sources में से एक है।

उदाहरण: Accessing Array Elements

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

// Program execution starts in main()
int main() {
	int scores[] = {90, 85, 78};
	// Print to the console with cout
	std::cout << "First: " << scores[0] << std::endl;
	// Print to the console with cout
	std::cout << "Last: " << scores[2] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Array Elements Modify करना

एक indexed position को सीधे एक नई value assign करना, जैसे scores[2] = 100;, वहाँ पहले जो भी store था उसे permanently overwrite कर देता है — arrays history preserve नहीं करते, इसलिए एक बार एक value replace हो जाए, original चला जाता है जब तक आपने इसे कहीं और save न किया हो।

उदाहरण: Modifying Array Elements

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

// Program execution starts in main()
int main() {
	int scores[] = {90, 85, 78};
	scores[2] = 100;
	// Print to the console with cout
	std::cout << scores[2] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

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

एक standard indexed for loop या एक range-based for loop दोनों order में एक array के हर element में चल सकते हैं; indexed version additionally आपको हर element की position देता है, जो range-based version सीधे expose नहीं करता।

Indexed form तब चुनें जब आपको actually जानना हो कि एक element *कहाँ* है, सिर्फ इसकी value नहीं।

उदाहरण: Looping Through Arrays

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

// Program execution starts in main()
int main() {
	int scores[] = {90, 85, 78};
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		// Print to the console with cout
		std::cout << scores[i] << " ";
	}
	// Print to the console with cout
	std::cout << 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. 5 size के एक array में arr[5] पढ़ना या लिखना, जब valid indexes 0 से 4 हैं, जो undefined behavior है।
  2. एक uninitialized local array इस्तेमाल करना और इसके elements print करना, जो garbage values दिखाता है।
  3. एक array को दूसरे को a = b; से assign करने की कोशिश करना, जो C++ में allowed नहीं है; इसकी बजाय element by element copy करें।

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.