A
A
Alexey Igoshev2015-09-27 18:17:13
C++ / C#
Alexey Igoshev, 2015-09-27 18:17:13

What are pointers for?

Good day to all. I can't figure out what pointers are for? Explain in simple, human language, please.
At first I myself thought that through pointers to change the values ​​of variables, but why do this through pointers, if you can directly assign a new value to this variable.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Stanislav Makarov, 2015-09-28
@Premium_gold

Well, I'll try it too.
Let's look at the question from the other side.
A variable is a container for a value. What do we generally need to work with a variable? What do we need to read/write a value from/to it? We need a data type and an address in memory.
The data type is a separate story, let's leave it aside for now. Let's just say that it defines the allowed operations on the value and the amount of memory required to store the value.
Let's talk about the address. In languages ​​where direct work with memory is available, any variable has its own address in memory. Also, in a slightly narrower sense, a variable is located in a memory area that is writeable. It doesn't really matter which data structure manages this memory - the stack or the heap - it is important that this memory is available at the time of use.
The correct address in memory is also the unique "key" of the variable, its distinguishing feature. When working with variables, a low-level language programmer inevitably works with addresses.
Another question is how to work with the address of a variable. When you create an ordinary local variable, the compiler takes part in working with it. When you write int a, the compiler (if you don't go into details) places a pair in its identifier table: (variable_name, memory_address). The usual local variable "a" (also called "automatic variable") is a way to create a variable by means of the compiler. The compiler will free the memory occupied by this variable when it goes out of scope. However, as long as you know for sure that this variable is "living", you can quite safely get its address using the & operation - the compiler will give it to you from its identifier table.
But "variables" in a broad sense can be created not only by means of the compiler, but also manually using malloc (C) or new (C++). These dynamic variables live as long as you need - you create them, you destroy them. The compiler knows nothing about these variables, because you create them dynamically at runtime. To access these variables, you also need an address, but you can't ask the compiler for it: therefore, you yourself need to save those addresses that the malloc function or the new operator returned to you. You can save this address in ANOTHER variable, and such a variable that stores addresses is a pointer (besides, if the pointer is not untyped (void*), then its type (float*) also tells us the type of the variable it points to ( float)).
It is very important that you can store the address of ANY variable in a pointer - both automatic, which the compiler created for you, and "manual" - which YOU created using malloc / new. And pass, for example, this address to the function. In fact, in the C language, pointers are the way to pass the VARIABLE ITSELF to the function, not its VALUE at the time of the call. There are more references in C++, but that's a separate story (references are a pointer "wrapped" in a regular identifier), ask a separate question about them.
Just the same, you will not assign "directly", because you can't have ALL the variables in a program in the same scope. If only because you can have dynamic variables, and the only way to work with them is to work through a pointer.
If you've never worked with dynamic memory, you may ask, what's the point of "manual" variables if you need a pair of pointer variables to store their address? And the whole point is that the address of a dynamically created variable can also be stored in a dynamically created pointer. This is where all the infinite freedom of building dynamic data structures opens up. If you have not heard of a linked list, then it's time to read a good book with examples. You can’t tell this in one answer, these are the basics of programming.

D
Daemon23RUS, 2015-09-27
@Daemon23RUS

For example, you use a variable in your program, when you call a function / procedure, you pass a copy of this variable, changes made in the procedure will not affect the main variable. By passing a pointer, the procedure can change the value of the main variable.
That's if you don't go into a lot of details...

M
Mercury13, 2015-09-27
@Mercury13

Okay I will try. Here we have a file on some NAS. We want to send it to a friend. We can…
• attach a file to a message, then we will have two independent files. Simple and reliable, two drawbacks. 1) The file is copied, it's time consuming. 2) The copy lives its own life, and changes in the copy will not affect the original. This is a transfer by copy.
• give a file name. One changes the file - the second will read the change, and no copying. The disadvantage is that it is necessary to negotiate who owns the file (i.e. destroy it when it is not needed). The filename is the pointer.
The metaphor is incomplete, because the file system can tell if there is a file, and in memory the pointer to the missing "file" is Access Violation. And there is no "account" in memory. However…

S
sitev_ru, 2015-09-28
@sitev_ru

It's like a shortcut in Windows! You can access the file directly, or you can place a shortcut, for example, on the desktop. The shortcut will indicate where the file is located.
So it is with pointers ...
Somewhere there is a variable, and the pointer has the address of the location of the variable in memory. And now it depends on the situation whether to access the variable directly or through a pointer ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question