Why are pointers needed in programming?

Is it important to use pointers when writing code in any language, for example, in C-language, it uses more memory.

Thanks u

+4
source share
4 answers

Ditto SpyrosP comments that this question is difficult to answer without going into a lengthy discussion.

My guess is that my short answer would be: Do I need pointers for programming? No. They solve some problems easier or cleaner, but you can always find alternative solutions. He likes to ask: "Are databases important for programming?" or even "Is the multiplication operator important for programming?" Take away any one or two functions, and you can almost always do the job the other way with the rest of the functions.

There are a number of examples in which pointers are useful.

For example, pointers are very useful when we want to create a relationship between two or more things, all of which can be updated independently. For example, we have a memory block with customer information and another memory block with order information. Order for some customers. We could copy all the customer information into the order block. But then, if customer information changes, we must change it in two places. What if we store several orders in memory, which may be for the same or different customers? Now, if the customer information is changed, we need to somehow find out what orders are associated with this customer, and change all of them. If we make a mistake, we may have conflicting customer information.

But with pointers, we can only have one copy of customer information, and orders have a pointer to the client. Then, if the customer information changes, we do not need to update another copy in each order, because there is no “other copy” in each order. All orders have a pointer to one copy. We change one place, and magically all other places see the same change.

You may want to get a book or find a website on data structures to get more examples.

+8
source

This is a pretty tricky question to answer without going into a long discussion. First of all, pointers are incredibly useful and necessary in order to make the language universal.

A very typical situation is when you want to pass a variable by reference. This means that you want to change its value inside the function. Passing only the variable name will only pass its value. Instead, you should pass a pointer that shows the passed address of the variable.

Or, when you need to point to the newly created memory node, as a linked list of node.

As for memory, the pointer is like another variable. Thus, if an integer variable contains 4 bytes of STORAGE (not memory), then the integer pointer for this variable contains 4 bytes of memory. This is not about memory, but about storage.

+1
source

No, pointers are not necessary for a programming language. There are languages ​​that don't have pointers: Java and Python are well-known examples. Almost all languages ​​using the functional paradigm do not have a notion of pointer.

The reason you need to work with pointers in C is because C is a relatively low-level language. It is best suited for writing firmware, drivers, OS components, or performance-critical libraries. In all these areas, you usually work a lot with blocks of memory and addresses, and pointers are an abstraction of a paragraph of memory.

+1
source

The pointer for the specified value is similar to the URL of a pointed page.

Pointers are needed as URLs.

About memory: the URL takes up much less memory than the specified page, this is the same for pointers

0
source

All Articles