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

The reference is the variable that stores the address of certain data. References have many familiar features with pointers. Here I will show how references work and the difference between them and pointers. Let’s start by creating. To declare the reference use the ampersand &:

python

The reference also has its name and type, but it doesn’t need to get the address of the variable using the additional operator &. Nevertheless, in both cases, we assign the address of the variable a to another variable (pointer or reference):

If we want to print created pointer and reference, we will get the following:

12
cout << pa << endl; cout << ra << endl;
copy

By outputting the reference, we got the value it stores since references immediately work with the value they point to. You cannot just add the asterisk * to the reference (like with pointers) to get the address, as such syntax will cause an error.

Pay attention that you cannot declare the reference without the variable it will point to.

python
question-icon

Declare the reference rx:

string ;

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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 2. Capitolo 6

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
References

The reference is the variable that stores the address of certain data. References have many familiar features with pointers. Here I will show how references work and the difference between them and pointers. Let’s start by creating. To declare the reference use the ampersand &:

python

The reference also has its name and type, but it doesn’t need to get the address of the variable using the additional operator &. Nevertheless, in both cases, we assign the address of the variable a to another variable (pointer or reference):

If we want to print created pointer and reference, we will get the following:

12
cout << pa << endl; cout << ra << endl;
copy

By outputting the reference, we got the value it stores since references immediately work with the value they point to. You cannot just add the asterisk * to the reference (like with pointers) to get the address, as such syntax will cause an error.

Pay attention that you cannot declare the reference without the variable it will point to.

python
question-icon

Declare the reference rx:

string ;

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

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

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