Array Declaration and Initialization
In C++, arrays are a collection of elements of the same type. Here are different ways to declare and initialize arrays:
- Declare an array: To declare an array, you specify the type of elements it will hold and the size of the array. For example:
TEXT/X-C++SRC
1int myArray[5]; // Declare an array of size 5
- Declare and initialize an array: You can also declare and initialize an array in a single statement. For example:
TEXT/X-C++SRC
1double prices[] = {10.99, 19.99, 7.99}; // Declare and initialize an array
- Declare and initialize an array with a constant size: If you know the size of the array at compile-time, you can use a constant to specify its size. For example:
TEXT/X-C++SRC
1const int SIZE = 3;
2string names[SIZE] = {"Alice", "Bob", "Charlie"}; // Declare and initialize an array with a constant size
- Declare and initialize an array of characters: You can declare an array of characters by specifying individual characters enclosed in single quotes. For example:
TEXT/X-C++SRC
1char vowels[] = {'a', 'e', 'i', 'o', 'u'}; // Declare and initialize an array of characters
The size of an array can be determined using the sizeof
operator. The size of the array is calculated by dividing the size of the entire array by the size of a single element. The following example demonstrates this:
TEXT/X-C++SRC
1#include <iostream>
2using namespace std;
3
4int main() {
5 int myArray[5]; // Declare an array of size 5
6
7 double prices[] = {10.99, 19.99, 7.99}; // Declare and initialize an array
8
9 const int SIZE = 3;
10 string names[SIZE] = {"Alice", "Bob", "Charlie"}; // Declare and initialize an array with a constant size
11
12 char vowels[] = {'a', 'e', 'i', 'o', 'u'}; // Declare and initialize an array of characters
13
14 cout << "Size of myArray: " << sizeof(myArray) / sizeof(myArray[0]) << endl;
15 cout << "Size of prices: " << sizeof(prices) / sizeof(prices[0]) << endl;
16 cout << "Size of names: " << sizeof(names) / sizeof(names[0]) << endl;
17 cout << "Size of vowels: " << sizeof(vowels) / sizeof(vowels[0]) << endl;
18
19 return 0;
20}
In this example, we declare different types of arrays and initialize them with values. Then, we use the sizeof
operator to determine the size of each array by dividing the size of the entire array by the size of a single element.
Feel free to experiment with different types of arrays and their initialization!
xxxxxxxxxx
20
using namespace std;
int main() {
int myArray[5]; // Declare an array of size 5
double prices[] = {10.99, 19.99, 7.99}; // Declare and initialize an array
const int SIZE = 3;
string names[SIZE] = {"Alice", "Bob", "Charlie"}; // Declare and initialize an array with a constant size
char vowels[] = {'a', 'e', 'i', 'o', 'u'}; // Declare and initialize an array of characters
cout << "Size of myArray: " << sizeof(myArray) / sizeof(myArray[0]) << endl;
cout << "Size of prices: " << sizeof(prices) / sizeof(prices[0]) << endl;
cout << "Size of names: " << sizeof(names) / sizeof(names[0]) << endl;
cout << "Size of vowels: " << sizeof(vowels) / sizeof(vowels[0]) << endl;
return 0;
}
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment