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

Kursinnehåll

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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 5

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

course content

Kursinnehåll

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

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 5
Vi beklagar att något gick fel. Vad hände?
some-alt