5

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

Thank u

Questioner
  • 233
  • 5
  • 13
  • possible duplicate of [What are the barriers to understanding pointers and what can be done to overcome them?](http://stackoverflow.com/questions/5727/what-are-the-barriers-to-understanding-pointers-and-what-can-be-done-to-overcome) – David Basarab Apr 15 '11 at 17:17

4 Answers4

9

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.

Jay
  • 26,876
  • 10
  • 61
  • 112
  • 1
    +1 Very good explanation. As i also say, handling a linked list where elements are pretty much "not there" yet, is an example of why pointers can be quite useful. You describe a full example here nicely. – Spyros Apr 15 '11 at 17:39
  • I agree, a linked list is another excellent example of the usefulness of pointers. I'm not sure what you mean by "not there". I'd think the key point is, "You want to maintain an ordered list, adding and deleting items, without having to move a lot of data around." – Jay Apr 26 '11 at 16:24
  • 1
    -1 Because while pointers are necessary as an implementation detail, the examples given do not require exposing them to the developer. Java, for example, has "references," essentially pointers, that are nothing more than an implementation detail. Despite not having pointers as a language feature, implementing a linked list, or linking between two objects without copying data is certainly possible, even easy. In that case, pointers are an abstraction. – Nateowami Mar 27 '17 at 05:52
  • @Nateowami I'd say a Java reference is a pointer. Sure, you could implement Java references without using the actual memory address, like it could be an index into an array. But who says that a pointer has to be an actual memory address? On Intel computers a memory address is given as an offset from a base register, which seriously muddies the difference between an "actual address" and an "index into an array" anyway. – Jay Mar 29 '17 at 13:32
  • 2
    I agree, a reference is basically a pointer. My point is that they're abstracted away. Once could just as easily ask "Why do we need to handle CPU registers?" In assembly you do. In higher-level languages this is abstracted away. Java" doesn't have pointers" in the same way that C "doesn't have CPU registers." What I fail to understand (though there must be a reason) is why some reasonably high-level languages (e.g. Go) expose pointers as a language feature. I can't think of a single instance where passing objects by reference and primitives by copy wasn't exactly what I wanted to do anyway. – Nateowami Mar 30 '17 at 02:21
  • "they're abstracted away" Well, sort of, okay. To my mind CPU registers have pretty much been totally abstracted away in any high level language, while the idea of "handles" or "references" is not very far from the idea of a pointer, and it is very natural to talk about them just like you would talk about pointers. But debating how much something "seems like" something else "to me" is probably not a productive debate. :-) I'll concede your point at least partially. – Jay Mar 30 '17 at 16:11
1

A pointer for a pointed value is like an URL for a pointed page.

Pointers are as necessary as URLs are.

About memory : an URL takes far less memory than the pointed page, it's exactly the same thing for pointers

Tristan
  • 8,733
  • 7
  • 48
  • 96
1

No, pointers are not unavoidably required for a programming language. There are languages which have no pointers: Java and Python are well-known examples. Many languages adopting functional paradigm don't have a notion of (built-in) pointer.

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

ulidtko
  • 14,740
  • 10
  • 56
  • 88
  • 4
    Of course they do have pointers, they are just not explicitly used. Objects are pointers. – Spyros Apr 15 '11 at 17:26
  • 2
    @Spyros: no, they're not. References are kind of like pointers, but in most languages objects are not pointers. Not even in C++ "objects are pointers". – R. Martinho Fernandes Apr 15 '11 at 17:27
  • @SpyrosP, this statement is highly arguable. Yes, some languages adopt pass-by-reference semantics for objects, but can you call references pointers too? are they really pointers? – ulidtko Apr 15 '11 at 17:30
  • And I'd add that "objects are pointers" is just plain wrong. *Names* are pointers, probably, but not objects, ever. – ulidtko Apr 15 '11 at 17:31
  • I think that the difference is very subtle, if any. References and pointers are pretty much the same thing to my eyes. Behind the scenes, i could be wrong, but i would say that Java and Python heavily utilize pointers(or references, if you like, still the same to me :P) – Spyros Apr 15 '11 at 17:34
  • 1
    @ulidtko: The statement is obviously an exaggeration about an object's correlation with a pointer (or reference if you like it more) .. – Spyros Apr 15 '11 at 17:35
  • 2
    Objects are not pointers, but in Java and C++ and various other languages, we use "handles" or "references" or whatever term they want to use, which are pointers to objects. I suppose you could make a technical quibble that an object reference is not necessarily an actual address but may instead be some other identifier of an object. That is, we could debate the exact definition of "pointer". But IN PRACTICE, an object reference looks like a pointer and acts like a pointer. It passes the "duck test": It's a pointer. – Jay Apr 15 '11 at 17:39
  • @SpyrosP, obviously you are confusing references, pointers, and memory addresses. – ulidtko Apr 15 '11 at 17:39
  • @ulidtko : I never mention a memory address.. Could you please indicate what the significant differences betweet a pointer and a reference are ? It's funny that 10 years of reverse engineering make me memory address ignorant still. And since i'm pretty sure that's what you will be doing, let me help you a bit : http://www.google.com/search?q=differences+between+pointers+and+references&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a – Spyros Apr 15 '11 at 17:41
  • 1
    @Jay, exactly. A reference is actually a pointer. To an object, but a pointer. – Spyros Apr 15 '11 at 17:43
  • @ulidtko: Computers pretty much confuse references, pointers, and memory addresses, too. :-) You may want to draw some technical semantic decision, but they're essentially the same concept. – Jay Apr 26 '11 at 16:27
  • @Jay, computers pretty much confuse integers with characters, decimal with octal, size with index, too. If you go reductionist, you can say that computers can only differentiate bit words, and confuse everything else. But that contradicts out intuitive, interpreted understanding of all those bitwords. Computers operate on numbers, but humans operate on *abstractions* over those numbers. So maybe *for computer* pointer do equals reference, but *for a programmer*, it does not. – ulidtko Apr 26 '11 at 18:01
-1

It's a pretty difficult question to answer without going into a long discussion. First of all, pointers are incredibly useful and required to make a language versatile.

A very typical situation is when you want to pass a variable by reference. Meaning that you want to change its value inside a function. Passing just the variable name, would only pass its value. Instead, you have to pass a pointer, which shows the passed variable address.

Or when you have to point to a new memory created node, like a linked list node.

As far as memory is concerned, a pointer is just like another variable. Thus, if an integer variable holds 4 bytes of STORAGE(not memory), then an integer pointer for that variable holds 4 bytes of storage. It's not about memory, it's about storage.

Spyros
  • 46,820
  • 25
  • 86
  • 129