Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Accessing Elements and Indexing Rules | Working with Array Elements
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
C++ Arrays

bookAccessing Elements and Indexing Rules

When you work with arrays in C++, you use indices to access individual elements. C++ uses zero-based indexing. This means the first element of an array is at index 0, the second at index 1, and so on. If an array has n elements, the valid indices go from 0 up to n - 1. Trying to access an index outside this range can cause problems in your program.

main.cpp

main.cpp

copy
123456789101112
#include <iostream> int main() { int numbers[5] = {10, 20, 30, 40, 50}; std::cout << "Element at index 0: " << numbers[0] << std::endl; std::cout << "Element at index 2: " << numbers[2] << std::endl; std::cout << "Element at index 4: " << numbers[4] << std::endl; // WARNING: The following line accesses out of bounds! std::cout << "Element at index 5 (out of bounds): " << numbers[5] << std::endl; }

In the code above, you see how to access elements at different positions in the array. Accessing numbers[0] gets the first element, numbers[2] gets the third, and numbers[4] gets the fifth. However, when the code tries to access numbers[5], it goes beyond the valid range of indices for a 5-element array. While the compiler might not give an error, this is unsafe: accessing an invalid index can lead to unpredictable results or even crash your program.

main.cpp

main.cpp

copy
123456789
// File: main.cpp #include <iostream> int main() { int data[4] = {7, 14, 21, 28}; int lastIndex = 4 - 1; // Calculate the last valid index std::cout << "Last element: " << data[lastIndex] << std::endl; }

A common mistake when working with arrays is the off-by-one error. This happens when you accidentally use an index that is one too high or too low, such as using the array's size as an index instead of size - 1 for the last element. To avoid these errors, always remember that array indices start at 0 and end at size - 1. Double-check your index calculations, especially when looping through arrays or accessing the last element.

Note
Note

Accessing an array out of bounds leads to undefined behavior in C++. This means the program may run incorrectly, crash, or appear to work but with hidden bugs. Always stay within the valid index range.

question mark

What is the valid index range for a 5-element array in C++?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 1

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you explain more about what happens if I access an invalid array index in C++?

How can I safely loop through all elements of an array in C++?

What are some tips to avoid off-by-one errors when working with arrays?

bookAccessing Elements and Indexing Rules

Glissez pour afficher le menu

When you work with arrays in C++, you use indices to access individual elements. C++ uses zero-based indexing. This means the first element of an array is at index 0, the second at index 1, and so on. If an array has n elements, the valid indices go from 0 up to n - 1. Trying to access an index outside this range can cause problems in your program.

main.cpp

main.cpp

copy
123456789101112
#include <iostream> int main() { int numbers[5] = {10, 20, 30, 40, 50}; std::cout << "Element at index 0: " << numbers[0] << std::endl; std::cout << "Element at index 2: " << numbers[2] << std::endl; std::cout << "Element at index 4: " << numbers[4] << std::endl; // WARNING: The following line accesses out of bounds! std::cout << "Element at index 5 (out of bounds): " << numbers[5] << std::endl; }

In the code above, you see how to access elements at different positions in the array. Accessing numbers[0] gets the first element, numbers[2] gets the third, and numbers[4] gets the fifth. However, when the code tries to access numbers[5], it goes beyond the valid range of indices for a 5-element array. While the compiler might not give an error, this is unsafe: accessing an invalid index can lead to unpredictable results or even crash your program.

main.cpp

main.cpp

copy
123456789
// File: main.cpp #include <iostream> int main() { int data[4] = {7, 14, 21, 28}; int lastIndex = 4 - 1; // Calculate the last valid index std::cout << "Last element: " << data[lastIndex] << std::endl; }

A common mistake when working with arrays is the off-by-one error. This happens when you accidentally use an index that is one too high or too low, such as using the array's size as an index instead of size - 1 for the last element. To avoid these errors, always remember that array indices start at 0 and end at size - 1. Double-check your index calculations, especially when looping through arrays or accessing the last element.

Note
Note

Accessing an array out of bounds leads to undefined behavior in C++. This means the program may run incorrectly, crash, or appear to work but with hidden bugs. Always stay within the valid index range.

question mark

What is the valid index range for a 5-element array in C++?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 1
some-alt