Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Pointers’ Arithmetic | References & Pointers
C++ Intermediate | Mobile-Friendly
course content

Contenu du cours

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

1. Data Types and Arrays
2. References & Pointers
3. Dynamic Memory
4. Functions

book
Pointers’ Arithmetic

Why did we go deep into arrays while learning pointers? The fact is that the array’s name is a pointer to its first element.

We can get the address of the first element of the array by its name or by declaring the pointer:

12345
int arr[5]{1, 2, 3, 4, 5}; int *p = &arr[0]; cout << p << endl; cout << arr << endl;
copy

In other words, indexing is equivalent to adding (or substructing) to the pointer:

12
cout << *(p+2) << endl; // equivalent to arr[2] cout << *(arr+2) << endl; // equivalent to arr[2]
copy

The for loop we used to go through the arrays can also be used with pointers by adding 1 on each step, but we will use these feature pointers in work with functions and dynamic memory in the following sections.

question mark

Which of the following statements can be used to get the second element of the array x?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 5

Demandez à l'IA

expand
ChatGPT

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

course content

Contenu du cours

C++ Intermediate | Mobile-Friendly

C++ Intermediate | Mobile-Friendly

1. Data Types and Arrays
2. References & Pointers
3. Dynamic Memory
4. Functions

book
Pointers’ Arithmetic

Why did we go deep into arrays while learning pointers? The fact is that the array’s name is a pointer to its first element.

We can get the address of the first element of the array by its name or by declaring the pointer:

12345
int arr[5]{1, 2, 3, 4, 5}; int *p = &arr[0]; cout << p << endl; cout << arr << endl;
copy

In other words, indexing is equivalent to adding (or substructing) to the pointer:

12
cout << *(p+2) << endl; // equivalent to arr[2] cout << *(arr+2) << endl; // equivalent to arr[2]
copy

The for loop we used to go through the arrays can also be used with pointers by adding 1 on each step, but we will use these feature pointers in work with functions and dynamic memory in the following sections.

question mark

Which of the following statements can be used to get the second element of the array x?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 5
Nous sommes désolés de vous informer que quelque chose s'est mal passé. Qu'est-il arrivé ?
some-alt