Sure, let's go over arrays in C++, including 1D and 2D arrays, and discuss the size limits for local and global arrays.
int arr1[5]; // Declares an array of 5 integers
int arr2[5] = {1, 2, 3, 4, 5}; // Declares and initializes an array
int arr3[] = {1, 2, 3, 4, 5}; // Compiler determines the size automatically
int firstElement = arr2[0]; // Accesses the first element
int lastElement = arr2[4]; // Accesses the last element
for (int i = 0; i < 5; ++i) {
std::cout << arr2[i] << " ";
}
int arr1[3][4]; // Declares a 3x4 array
int arr2[3][4] = { // Declares and initializes a 3x4 array
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int arr3[][4] = { // Compiler determines the size of the first dimension
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int firstElement = arr2[0][0]; // Accesses the element at the first row, first column
int lastElement = arr2[2][3]; // Accesses the element at the last row, last column
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 4; ++j) {
std::cout << arr2[i][j] << " ";
}
std::cout << std::endl;
}
void someFunction() {
int largeArray[100000]; // Risk of stack overflow
}