P
P
Pinkman2021-08-14 10:21:40
C++ / C#
Pinkman, 2021-08-14 10:21:40

What is the difference between * and &?

Please tell me what is the difference between these two declarations:

Test *a;
Test &b;

I, as a person who studied C, do not understand ... for me it all looks like a pointer ... but they say that there is a difference.
I understand that the first variable stores a pointer, and the second stores the address (but isn't it the same pointer??)
If there is a difference, then what is the variable address declaration used for?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Zagaevsky, 2021-08-14
@famousman204

A pointer is an address, and it is stored in a variable.
& This is a link. Like an alias to the place it refers to. There is a difference.
The reference cannot be changed, only the value in memory where the reference refers to can be changed. The link itself cannot be changed. The pointer can be changed. A constant pointer cannot be changed.
A reference cannot be uninitialized, a pointer can be.
A pointer can point to memory on the heap that must be freed, a reference never needs to be freed separately.
Plus, there are also r-value references (I think that's what they're called) &&. They cannot be assigned, only read.

E
Evgeny Zaletsky, 2021-08-14
@JZ_52

Here is some information about your question.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question