Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why pointers are necessary in programing? [closed]

Is it important to use pointers while writing a code in any language for example C-language does it utilize more memory.

Thank u

like image 433
Questioner Avatar asked Apr 15 '11 17:04

Questioner


People also ask

Why pointer is necessary in any programming language?

Pointers are used to store and manage the addresses of dynamically allocated blocks of memory. Such blocks are used to store data objects or arrays of objects.

Are pointers necessary?

Pointers are extremely important, which allows us to access addresses and manipulate their contents. Pointer is also the most complex and difficult feature in C/C++ language. If we use pointers correctly, pointers can widely improve efficiency and performance.

Why pointers are needed explain with example?

A pointer is a variable that stores the address of another variable. Unlike other variables that hold values of a certain type, pointer holds the address of a variable. For example, an integer variable holds (or you can say stores) an integer value, however an integer pointer holds the address of a integer variable.

Why do we need pointer in data structure?

Why do We Need Pointers in Data Structure? Optimization of our code and improving the time complexity of one algorithm. Using pointers helps reduce the time needed by an algorithm to copy data from one place to another.


1 Answers

Ditto SpyrosP's comment that this question is difficult to answer without going into a long discussion.

I guess my short answer would be: Are pointers absolutely necessary to programming? No. They make some problems easier or cleaner to solve, but you could always find alternative solutions. It's like asking, "Are databases important to programming?" or even "Is a multiplication operator important to programming?" Take away any one or two features and you could almost always get the job done some other way with the remaining features.

There are a number of examples where pointers are useful.

For example, pointers are very useful when we want to create an association between two or more things, all of which might be updated independently. Like, say we have a block of memory with information about a customer, and another block of memory with information about an order. The order is for some customer. We could copy all the customer information into the order block. But then if the customer information changes, we have to change it in two places. What if we are keeping several orders in memory, which might be for the same or different customers? Now if the customer information is changed, we have to somehow know which orders related to that customer, and change all of them. If we make a mistake doing this, we could have contradictory customer information.

But with pointers, we could have just one copy of the customer information, and the orders have a pointer to the customer. Then if the customer information changes, we don't need to update another copy in each order because there is no "other copy" in each order. The orders all just have a pointer to the one copy. We change one place, and magically all the other places see that same change.

You might want to get a book or find a web site about data structures to get more examples.

like image 146
Jay Avatar answered Sep 24 '22 19:09

Jay