What is a pointer?

See: Index Information


Pointers can be used in many C flavoring languages ​​and some older languages ​​such as Fortran.

As someone who is really programmed mainly in javascript and actionscript, can you explain to me what a pointer is and what it is most useful for?

Thanks!

+4
source share
5 answers

Pointers are not as strong as they sound. As others have said, they are variables that contain the address of some other variable. Suppose I wanted to give you directions to my house. I would not give you a photograph of my house or a scale model of my house; I would just give you the address. You could conclude what you need.

In the same way, many languages ​​distinguish between passing by value and passing by reference. Essentially, does this mean that I will pass the whole object around every time I need to reference it? Or, I just give out his address so that others can conclude what they need?

Most modern languages ​​hide this complexity by figuring out when pointers are useful and optimizing it for you. However, if you know what you are doing, manually manipulating pointers can be useful in some situations.

+3
source

This article will provide detailed information on what a pointer is:

In computer science, a pointer is a data type of a programming language whose value refers directly to (or "points to") another value stored elsewhere in the computer's memory using its address. Getting or querying the value that the pointer refers to is called pointer dereferencing. A pointer is a simple implementation of a common reference data type (although it is very different from an object referred to as a reference in C ++). Data pointers improve performance for repetitive operations, such as moving string and tree structures, and function pointers are used for binding methods in object-oriented programming and runtime associated with dynamic link libraries (DLLs).

+8
source

A pointer is a variable containing the address of another variable. This allows you to indirectly refer to another variable. For example, in C:

// x is an integer variable int x = 5; // xpointer is a variable that references (points to) integer variables int *xpointer; // We store the address (& operator) of x into xpointer. xpointer = &x; // We use the dereferencing operator (*) to say that we want to work with // the variable that xpointer references *xpointer = 7; if (5 == x) { // Not true } else if (7 == x) { // True since we used xpointer to modify x } 
+6
source

There were several discussions on SO on this topic. You can find information on this topic with the links below. There are several other relevant discussions on this subject, but I believe that they were the most important. Search for “[C ++] pointers” in the search box (or “[c] pointers”) and you will get more information.

In C ++ I can't grab pointers and classes

What is the difference between modern "guidelines and traditional" pointers?

+1
source

As already mentioned, a pointer is a variable containing the address of another variable.

It is mainly used when creating new objects (at run time).

-1
source

Source: https://habr.com/ru/post/1277326/


All Articles