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

Contenuti del Corso

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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 5

Chieda ad AI

expand
ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

course content

Contenuti del Corso

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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 5
Siamo spiacenti che qualcosa sia andato storto. Cosa è successo?
some-alt