To explore more information about initializing arrays in C++, understanding array pointers, please continue reading this article by Mytour. Additionally, readers can refer to the previous article on arrays in C++ (Part 1) to learn more about how to declare arrays in C++.
Article Contents:
1. Initializing Arrays in C++
2. Array Pointers
1. Initializing Arrays in C++
The structure for initializing arrays looks like int n[] = {2, 4, 8};
However, when declaring an array, for example, int n[3];, we need to assign values to the array separately. Because 'int n[3];' allocates memory for 3 integers but does not contain any values.
To assign values to the array, we assign values to each element of the array individually.
Assign the value 2 to n[0];
Assign the value 4 to n[1];
Assign the value 8 to n[2];
Similar to how we declare variables and then assign values to those variables.
Declare variables x, y, and z as integers;
Initialize x with the value 2;
Initialize y with the value 4;
Initialize z with the value 8;
So there are two ways to initialize an array, which include:
int n[] = {2, 4, 8};
The second approach is to declare an array and then assign values to each element in that array:
int n[3];
Assign the value 2 to n[0];
Assign the value 4 to n[1];
Assign the value 8 to n[2];
To make it easier to visualize, let's consider n[0], n[1], and n[2] as different variables that we have used before.
Similar to a variable, an array can be of any data type.
float f[] = {1.1, 1.4, 1.5};
Here, f is an array of type float.
- Example 1:
Firstly, let's consider an example of calculating the average score for 3 students. Here, score[0], score[1], score[2] correspond to the scores of the first, second, and third students.
#include
int main(){
using namespace std;
Declare an array marks of size 3;
Declare a float variable trung binh;
cout < 'Enter the marks for the first student:' <<
cin >> marks[0];
cout < 'Enter the marks for the second student:' <<
cin >> marks[1];
cout < 'Enter the marks for the third student:' <<
cin >> marks[2];
average = (marks[0] + marks[1] + marks[2]) / 3.0;
cout < 'average score: ' << average <<
return 0;
}
Output Format:
Enter the score for the first student
23
Enter the score for the second student
25
Enter the score for the third student
31
Average Score: 26.3333
In the above example, we handle the array similar to how we handle regular variables. Additionally, there are two points to note:
- The average value must be of data type float because the average of integers can also be a float.
- When calculating the average score, the sum of the scores must be divided by 3.0 instead of 3, otherwise, the average score you get may not be a float.
- Example 2: In this example, we use a loop:
#include
int main(){
using namespace std;
int n[10]; /* declare n as an array of 10 integers */
int i,j;
/* initialize the elements of n */
for ( i = 0; i<10; i++=''>
{
cout < 'Enter the value of n['<< i <<']: ' << endl;
cin >> n[i];
}
/* print the values of array elements */
for (j = 0; j < 10;='' j++=''>
{
cout < 'n['<< j <<']=' << n[j] << endl;
}
return 0;
}
- Output Format:
Enter the value of n[0]
23
Enter the value of n[1]
25
Enter the value of n[2]
31
Enter the value of n[3]
1
Enter the value of n[4]
33
Enter the value of n[5]
35
Enter the value of n[6]
76
Enter the value of n[7]
47
Enter the value of n[8]
74
Enter the value of n[9]
45
n[0] = 23
n[1] = 25
n[2] = 31
n[3] = 1
n[4] = 33
n[5] = 35
n[6] = 76
n[7] = 47
n[8] = 74
n[9] = 45
In the above program, the code is quite simple, I and j start from 0 because array indices start from 0 and go up to 9 (for 10 elements). So, i and j increment up to 9, not 10 (i <10 and j <10). Therefore, in the code snippet, n[i] will be n[0], n[1], n[2], ..., n[9].
Furthermore, in the above example, we can observe 2 loops. In the first for loop, we take the values of different elements of the array one by one. In the second for loop, we print the values of array elements.
In the first iteration, the value of i is 0, so n[i] is n[0]. Therefore, by writing cin >> n[i];, the user will be prompted to enter the value of n[0]. Similarly, in the second iteration, the value of i will be 1, and n[i] will be n[1]. So cin >> n[i]; will be used to input values from the user for n[1], ... . The value of i will increment up to 9, covering array indices (0,1,2, ..., 9).
Arrays allocate contiguous memory. So, if the address of the first element of an integer array is X, then the address of the second element will be X + 4 (4 is the size of an integer), and the third element will be X + 4 + 4, ... . In other words, the memory for all elements of an array is allocated together and continuously.
2. Pointers to Arrays
Next, let's explore how to pass pointers to an array. Before starting, assume that we have accessed a pointer.
If not familiar, a pointer is a variable with a value that is the address of another variable, for example, if a variable y points to another variable x, meaning the value of variable y is the address of variable x.
Similarly, if a variable y points to an array n, meaning the value of variable y is the address of the first element of the array, i.e., n[0]. So, y is a pointer to the array n. The array's name is a pointer to the first element of the array.
If p is a pointer to an array of ages, meaning p (or ages) points to ages[0].
int ages[50];
int *p;
p = ages;
In the above code snippet, assign the address of the first element of ages to p. Since p points to the first element of the ages array, *p is the value of the first element of the array.
As *p refers to the first array element, *(p+1) and *(p+2) correspond to the second and third elements, respectively. So *p is ages[0], *(p + 1) is ages[0], *(p + 2) is ages[0], ....
Similarly, *ages is ages[0] (the value of ages), *(ages+1) is ages[1] (the value of ages+1), *(ages+2) is ages[2] (the value of ages+2), ... .
Above are details about pointers to arrays in C++. Refer to the example below to gain a deeper understanding of pointers to arrays:
#include
int main(){
float n[5] = { 20.4, 30.0, 5.8, 67, 15.2 }; /* declare n as an array of 5 floats */
float *p; /* p is a float pointer */
int i;
p = n; /* p points to array n */
/* print values of array elements */
for (i = 0; i < 5; i++)
{
std::cout < '*(p += '< i < ')=' << *(p + i) << std::endl;/* *(p+i) is the value of (p+0),(p+1)... */
}
Return 0;
}
Output format:
*(p + 0) = 20.4
*(p + 1) = 30
*(p + 2) = 5.8
*(p + 3) = 67
*(p + 4) = 15.2
Since p is pointing to the first element of the array, *p or *(p+0) corresponds to the value at p[0] or the value at the first element of p. Similarly, *(p+1) corresponds to the value at p[1]. So, *(p+3) and *(p+4) correspond to the values at p[3] and p[4].
The example synthesizes all the concepts. Finally, print the addresses of the arrays and each element of the array.
#include
int main(){
int n[4] = { 20, 30, 5, 67 }; /* declare n as an array of 4 integers */
int *p; /*pointer*/
int i;
p = n; /*p points to array n*/
/* print the address of the array */
std::cout << ' address of array n = ' << p << std::endl; /*p points to the array, storing the address of the first element of the array*/
/* print addresses of array elements */
for (i = 0; i < 4; i++ )
{
std::cout << ' address of n['<< i <<']=' << &n[i] << std::endl;
}
return 0;
}
Output format:
address of array n = 0xfffe2c0c
address of n[0] = 0xfffe2c0c
address of n[1] = 0xfffe2c10
address of n[2] = 0xfffe2c14
address of n[3] = 0xfffe2c18
In the above example, the address of the first element of n and p is the same. Additionally, we print the values of different elements of the array using (p + 1), (p + 2), and (p + 3).
So in the article on arrays in C++ (part 1) here, Mytour has just introduced you to initializing arrays as well as pointers to arrays in C++. In the upcoming article on arrays in C++ (part 3), Mytour will further introduce you to how to pass an entire array to a function, as well as what a for-each loop is.
' ]=''>{C}{C}{C}{C}{C}
}
return 0;
}
Output format:
address of array n = 0xfffe2c0c
address of n[0] = 0xfffe2c0c
address of n[1] = 0xfffe2c10
address of n[2] = 0xfffe2c14
address of n[3] = 0xfffe2c18
In the above example, the address of the first element of n and p is the same. Additionally, we print the values of different elements of the array using (p + 1), (p + 2), and (p + 3).
So in the article on arrays in C++ (part 1) here, Mytour has just introduced you to initializing arrays as well as pointers to arrays in C++. In the upcoming article on arrays in C++ (part 3), Mytour will further introduce you to how to pass an entire array to a function, as well as what a for-each loop is. Stay tuned for a detailed presentation of this content in Part 3 of Arrays in C.
