Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Pointers | 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

Each variable has its address in memory, a storage unit where it "lives" during the program's execution. To access the address, you can use ampersand &. For instance:

12
int x; cout << &x;
copy

It means that the address of the variable x for this execution of your code is 0x7ffd586e0344. With each execution, the address can change since the program can start at any part of your memory.

A pointer is a variable that stores the address of another variable. To declare the pointer use the asterisk *. For example, let's declare a pointer to the variable type of double:

python

Like with variables, we have to name the pointer and define the type it points to by declaration.

Let's learn how to assign the address of the variable to a pointer. For example, we want to know where the variable b is. Let's write down its address to the pointer pb:

python

To assign to the pointer the variable address, use the pointer's name without *. Or you can define the pointer by the declaration:

python

1. What is the pointer?

2. Output the address of the variable x.

question mark

What is the pointer?

Select the correct answer

question-icon

Output the address of the variable x.

int x = 2;
int *px = &x;

cout <<
;

Click or drag`n`drop items and fill in the blanks

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 1

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

Each variable has its address in memory, a storage unit where it "lives" during the program's execution. To access the address, you can use ampersand &. For instance:

12
int x; cout << &x;
copy

It means that the address of the variable x for this execution of your code is 0x7ffd586e0344. With each execution, the address can change since the program can start at any part of your memory.

A pointer is a variable that stores the address of another variable. To declare the pointer use the asterisk *. For example, let's declare a pointer to the variable type of double:

python

Like with variables, we have to name the pointer and define the type it points to by declaration.

Let's learn how to assign the address of the variable to a pointer. For example, we want to know where the variable b is. Let's write down its address to the pointer pb:

python

To assign to the pointer the variable address, use the pointer's name without *. Or you can define the pointer by the declaration:

python

1. What is the pointer?

2. Output the address of the variable x.

question mark

What is the pointer?

Select the correct answer

question-icon

Output the address of the variable x.

int x = 2;
int *px = &x;

cout <<
;

Click or drag`n`drop items and fill in the blanks

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

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