L
L
Legebocker2017-08-24 09:02:43
Programming
Legebocker, 2017-08-24 09:02:43

What is the difference between pointers and links?

I don’t understand. What is the difference between pointers and references? It seems that both do the same thing. It seems that both can be thrown into dynamic memory. What is their difference? Maybe something more productive? I only see the difference that the syntax of the link is more readable.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
M
Maxim Moseychuk, 2017-08-24
@fshp

A reference is an analogue of a constant pointer, which throws an exception when trying to initialize it to zero.

D
Dmitry Makarov, 2017-08-24
@DmitryITWorksMakarov

That's the difference. In syntax.
You can still do all kinds of address arithmetic with pointers.
As a C# programmer, it’s more convenient for me to work with references than to parse the furious syntax with pointers: small such dots are better, and not all this (*->&)

A
Alexander Titov, 2017-08-24
@alex-t

As a matter of fact - there and there the address. But the reference points to an already existing object, and the pointer can be null, which is sometimes convenient. It is easier to make a pointer invalid, pointing "to the garbage", a link is somewhat more complicated, but it is not protected from this either.
By the way, you cannot build a link directly to dynamic memory, first you have to create such an object by pointer.
In my opinion, in cases where there is no special value for a null pointer, references can be used.

D
Derevyanko Alexander, 2017-08-24
@dio4

references were invented in C++ for those who could not deal with pointers while studying (or not studying at all) the C programming language ;-) here
is more fully stated

A
asd111, 2017-08-24
@asd111

The difference is primarily in the application.
So it is customary to pass incoming parameters for a function by a constant reference, and it is customary to return outgoing parameters as a pointer.
Something like this: A pointer in the form of an input parameter can be used if you sometimes need to pass null to a function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question