Arrays in C++ are contiguous blocks of memory that hold elements of the same data type. They provide a way to store and access multiple values using a single variable name and an index.
Static arrays have a fixed size that is determined at compile time. The size must be a constant expression.
Here are several ways to declare and initialize static arrays in C++:
#include <iostream>
int main() {
int numbers[5]; // Declares an integer array named 'numbers' that can hold 5 elements
// You can then assign values to the elements using their index (0-based)
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
std::cout << "Element at index 2: " << numbers[2] << std::endl;
return 0;
}
#include <iostream>
int main() {
int scores[3] = {85, 92, 78}; // Declares and initializes an integer array with 3 elements
std::cout << "Score at index 1: " << scores[1] << std::endl;
return 0;
}
If you provide fewer initializers than the size of the array, the remaining elements are default-initialized (usually to 0 for numeric types).
#include <iostream>
int main() {
double values[5] = {3.14, 2.71}; // First two elements are initialized, the rest are 0.0
std::cout << "Value at index 0: " << values[0] << std::endl;
std::cout << "Value at index 3: " << values[3] << std::endl;
return 0;
}
If you provide an initialization list, you can omit the size of the array. The compiler will deduce the size based on the number of initializers.
#include <iostream>
int main() {
char message[] = {'H', 'e', 'l', 'l', 'o'}; // Size is automatically 5
std::cout << "Message: ";
for (char c : message) {
std::cout << c;
}
std::cout << std::endl;
int numbers[] = {100, 200, 300}; // Size is automatically 3
std::cout << "Number at index 2: " << numbers[2] << std::endl;
return 0;
}
You access individual elements of an array using their index, which starts from 0 for the first element and goes up to size - 1 for the last element.
#include <iostream>
int main() {
int data[] = {5, 10, 15, 20};
std::cout << "First element: " << data[0] << std::endl;
std::cout << "Second element: " << data[1] << std::endl;
std::cout << "Last element: " << data[3] << std::endl;
// Be careful not to access elements outside the array bounds (e.g., data[4] in this case),
// as it leads to undefined behavior.
return 0;
}
Dynamic arrays have their size determined at runtime. They are created using dynamic memory allocation (new
and delete[]
).
#include <iostream>
int main() {
int size;
std::cout << "Enter the size of the array: ";
std::cin >> size;
int* dynamicArray = new int[size]; // Allocate an integer array of the specified size on the heap
// Initialize the elements
for (int i = 0; i < size; ++i) {
dynamicArray[i] = i * 10;
}
// Access and print the elements
std::cout << "Elements of the dynamic array: ";
for (int i = 0; i < size; ++i) {
std::cout << dynamicArray[i] << " ";
}
std::cout << std::endl;
delete[] dynamicArray; // Deallocate the dynamically allocated memory
dynamicArray = nullptr; // Good practice to set the pointer to nullptr
return 0;
}
new[]
operator.delete[]
operator when it's no longer needed to prevent memory leaks.