Arrays in C++
What is an array?
An array stores many values of the same type in one named block, reached by an index. Instead of m1, m2, m3, m4, m5, you write int marks[5]; and use marks[0] to marks[4].
Declaring and initialising
int a[5]; // five ints, uninitialised
int b[5] = {1, 2, 3, 4, 5}; // with values
int c[5] = {1, 2}; // rest become 0
int d[] = {10, 20, 30}; // size inferred as 3Accessing elements
Indexes run from 0 to size − 1. The first is a[0]; the last of a 5-element array is a[4].
int b[5] = {1, 2, 3, 4, 5};
cout << b[0] << " " << b[4] << "\n"; // 1 5
b[2] = 99; // change an elementLooping through an array
int b[5] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 5; i++) sum += b[i];
cout << "Sum = " << sum << "\n";
for (int x : b) cout << x << " "; // range-based forSum = 15
1 2 3 4 5
2D arrays
A 2D array is a grid with two indexes — row and column:
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) cout << grid[i][j] << " ";
cout << "\n";
}1 2 3
4 5 6
A note on std::vector
Built-in arrays have a fixed size and do not track their own length. For most real code, C++ programmers prefer std::vector, which grows as needed and knows its .size(). See the vector lesson.
Learn arrays to understand the basics, but reach for std::vector when you actually build programs.
Common mistakes
- Going out of bounds — using
a[5]on a 5-element array. - Assuming an array knows its length inside a function (it does not).
- Looping with
<=size instead of<size. - Forgetting that uninitialised array elements hold garbage.
Read five numbers into an array, then print the largest and the average. Then make a 3×3 grid and print its diagonal (grid[0][0], grid[1][1], grid[2][2]).
Summary
- An array holds many same-type values, accessed by index from 0.
- Initialise with braces; missing values become 0.
- Loop with a classic
foror a range-based for. - 2D arrays use two indexes for rows and columns.
- Prefer
std::vectorfor real programs — it is safer and resizable.
Frequently Asked Questions
What is an array in C++?
int marks[5]; holds five integers, reached with marks[0] through marks[4]. Its size is fixed when you declare it.How do you declare and initialise an array in C++?
int a[5];, and can initialise with braces at the same time, like int a[5] = {1, 2, 3, 4, 5};. If you supply fewer values than the size, the remaining elements are set to zero.Why do array indexes start at 0 in C++?
a[0] is the first item and the last item of an array of size n is a[n-1].What is the difference between an array and a vector in C++?
std::vector can grow and shrink at run time, knows its size with .size(), and is generally safer and more convenient, which is why modern C++ often prefers it.What happens if you access an array out of bounds in C++?
Array क्या है?
Array एक ही type की कई values को एक नामित block में रखता है, index से पहुँचा जाता है। m1, m2, m3, m4, m5 के बजाय, आप int marks[5]; लिखते हैं और marks[0] से marks[4] इस्तेमाल करते हैं।
Declare और initialise करना
int a[5]; // paanch ints, uninitialised
int b[5] = {1, 2, 3, 4, 5}; // values ke saath
int c[5] = {1, 2}; // baaki 0 ho jaate hain
int d[] = {10, 20, 30}; // size 3 samajh liya jaata haiElements access करना
Indexes 0 से size − 1 तक चलते हैं। पहला a[0] है; 5-element array का आख़िरी a[4] है।
int b[5] = {1, 2, 3, 4, 5};
cout << b[0] << " " << b[4] << "\n"; // 1 5
b[2] = 99; // element badleinArray पर loop
int b[5] = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < 5; i++) sum += b[i];
cout << "Sum = " << sum << "\n";
for (int x : b) cout << x << " "; // range-based forSum = 15
1 2 3 4 5
2D arrays
2D array दो indexes वाला grid है — row और column:
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) cout << grid[i][j] << " ";
cout << "\n";
}1 2 3
4 5 6
std::vector पर एक नोट
Built-in arrays का size स्थिर होता है और वे अपनी लंबाई नहीं जानते। अधिकांश असली code के लिए, C++ programmers std::vector पसंद करते हैं, जो ज़रूरत अनुसार बढ़ता है और अपना .size() जानता है। vector lesson देखें।
Basics समझने को arrays सीखें, पर जब आप वाकई programs बनाएँ तब std::vector की ओर बढ़ें।
आम गलतियाँ
- Bounds के बाहर जाना — 5-element array पर
a[5]इस्तेमाल करना। - यह मानना कि array function के अंदर अपनी लंबाई जानता है (नहीं जानता)।
- size के साथ
<के बजाय<=से loop करना। - यह भूलना कि uninitialised array elements garbage रखते हैं।
पाँच numbers array में पढ़ें, फिर सबसे बड़ा और औसत print करें। फिर एक 3×3 grid बनाएँ और उसका diagonal print करें (grid[0][0], grid[1][1], grid[2][2])।
सारांश
- Array कई same-type values रखता है, 0 से index द्वारा access।
- Braces से initialise करें; छूटी values 0 हो जाती हैं।
- Classic
forया range-based for से loop करें। - 2D arrays rows और columns के लिए दो indexes इस्तेमाल करते हैं।
- असली programs के लिए
std::vectorपसंद करें — यह सुरक्षित और resizable है।