Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I often struggle to see the advantages of pointers (except for low level programming).

Why use of char* instead of a String or char[] or what advantages pointer arithmetic brings.

So what are the pros and use cases of pointers?

share|improve this question

closed as too broad by gnat, Kilian Foth, Dan Pichelman, amon, MichaelT Mar 4 at 0:17

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

6  
This is going to sound a bit elitist, but IMHO if you have to ask about pros and cons of pointers, you most likely don't need them. –  Jas Nov 2 '10 at 10:15
2  
Probably! But the question is still valid! –  Zolomon Nov 2 '10 at 10:51
1  
This also isn't really language-agnostic, as not all that many programming languages have real pointers in the C sense. –  David Thornley Nov 2 '10 at 14:01

8 Answers 8

up vote 3 down vote accepted

Pointers are necessary for dynamic memory location, many data structures, and efficient handling of large amounts of data. Without pointers, you'd have to allocate all the program data globally or in functions or the equivalent, and you'd have no recourse if the amount of data grew beyond what you had originally allowed for. I hesitate to use absolutes here, but as far as I know all modern computer languages have pointers in some form or other.

In most languages that use pointers, there are certain sorts of references that are pointers, and perhaps certain sorts of references that aren't, and there is no further notational difference. A Lisp cons cell is a pair of pointers, although a fixnum is not a pointer. In Java, the variable used for the instance of a class is a pointer, but an int isn't. The language syntax doesn't reflect that.

C is unusual in that pointers are optional, explicit, and allow explicit pointer arithmetic. It is perfectly possible to write struct foo bar; struct foo * baz;, and once you've allocated memory for baz you can use both bar and baz to represent struct foos. Since pointers are optional, it is useful to have notational differences. (It's essential in C++ for smart pointers, as given boost::shared_ptr<foo> bar;, bar.reset() has one meaning and bar->reset() is likely to have a much different one.)

(Actually, explicit pointers were often used in other languages when C was originally being developed, such as ^ in Pascal. C is an older language than most in common use today, and it shows.)

One of C's design goals was to write Unix in, and therefore it needed to handle memory locations in a detailed manner. (C is actually one of a family of system implementation languages common when it was being designed, another example being Cybol for Control Data computers. C is the one that became a big hit.) Therefore, it is possible to manipulate C pointers directly, assigning memory addresses and calculating new ones. This also led to some design decisions in C. C arrays are based heavily on pointer arithmetic, and indeed an array decays into a pointer in very many situations. Passing variables to C functions by reference is done by pointer. There was no strong need for arrays and passing variables by reference in the form that other contemporary languages had, so C didn't get those.

So, the answer is that, in most languages nowadays, you use pointers constantly without being reminded of the fact. In C, and to a lesser extent C++, you use pointers either to do low-level things, or as accomplish higher-level things that there's no special notation for.

share|improve this answer
    
very nice answer –  Kate Gregory Nov 2 '10 at 15:20
1  
I accept this answer because it describes best the difference I couldn't grasp between pointers in C/C++ and Reference in Java. –  OliverS Nov 16 '10 at 8:53
    
It would be good to add a short explanation of why Java doesn't use pointers to better clarify the perceived "con" by Gosling (Java's principal creator). –  Guido Anselmi Jul 3 at 14:55

Complex data structures. You can't build something like a linked list or a binary tree without pointers.

There are no "pros" and "cons" of pointers. They are just a tool, like a hammer.

share|improve this answer
4  
In Java, there is no pointers. Yet, it is possible to build linked list and binary tree. There is a difference between references (as in Java) and pointers (as in C). –  StartClass0830 Nov 2 '10 at 9:44
7  
There is no difference between references and pointers. Pointer arithmetic is C-specific; there are other languages that have pointers, but don't have pointer arithmetic (e.g., Pascal). –  zvrba Nov 2 '10 at 9:52
1  
Pointers and references are not synonymous as you think. Read this to see the differences. stackoverflow.com/questions/57483/… –  StartClass0830 Nov 2 '10 at 10:42
4  
To address the first two items of that list: Java references CAN be reassigned and they CAN point to null. The distinguishing feature of a pointer is that allows you to indirectly reference another object. My litmus test is: if you can build circular data structure (which you can with Java references), it is a pointer. (Note that you can't build circular data structure with C++ references.) –  zvrba Nov 2 '10 at 11:58
1  
There is a difference between pointers and references. C#, for instance, has both. (blogs.msdn.com/b/ericlippert/archive/2009/02/17/…) –  Steve Evers Nov 2 '10 at 16:02
  • With pointers you can allocate and deallocate memory in runtime.
  • And you can use large data-structures outside it's allowed scope without being copied.

References in C++, Java and other same type of languages are just 'safe pointers'. And these references are used a lot in Java.

share|improve this answer
1  
References in C++ are NOT safe pointers. References in C++ have nothing at all to do with pointers. References in C++ are aliases. They cannot be assigned to NULL, and they cannot be modified after they are created. –  Billy ONeal Nov 2 '10 at 13:16
    
@Billy: C++ references are usually implemented using pointers, and work similarly in some things, so people keep thinking of them as some sort of constrained pointer. –  David Thornley Nov 2 '10 at 13:59

char* is a crummy example of pointers. You are probably better off using std::string (or some better type that handles your unicode/ansi/multibyte specialness) than char*. For almost any other example of pointers (Employee*, PurchaseOrder*, ...), however, there are many advantages:

  • scope larger than a single function - allocate the object on the heap and pass the pointer around for a long time
  • quicker function calls for large objects since you don't have the copy cost of pass-by-value
  • one way to enable a function to change the parameters passed to it
  • save space and time in collections copying only an address instead of an entire object

In fact, pointers are so important that most languages that appear not to have them actually have only them. Reference types in C# and Java are essentially pointers disguised as solid objects.

Now, pointer manipulation (p++ on a pointer, or p += delta) is a whole 'nother story. Some people think it's dangerous, some people think it's great. But that's even further from your question.

share|improve this answer

Pretty much any computer program needs to inspect and change values in memory (known as peeking and pokeing, to those of us who are old enough). You need to control where in memory those values are, so that the outcome is predictable (and in certain cases, the order is important: loading executable code is one example). Therefore you need to have a data type that represents a location in memory. Even if your programming environment hides that under an abstraction, it's still there.

share|improve this answer
    
+1 for the BASIC reference - my first language! –  Michael K Nov 2 '10 at 15:22

The best answer is actually included in the question: pointers are for low-level programming. Granted, if you're using C, not using pointers is like programming with one hand tied behind your back, but the answer to that is to use a higher-level language instead.

share|improve this answer

Pointers can be faster and can incur less overhead, both in data structures and in keeping the program execution footprint down. (Please note the word 'can'.)

In general the rule is, if you allocated a resource, either by performing your own allocation or having something do it on your behalf, then it's your job to release it when done.

The burden of doing the above is placing the responsiblity back on to the developer, rather than having the runtime do it. This has some more advantages in that things can be longer lived, or cross boundaries, or be disposed of at more opportune times, or not need to carry the weight of a garbage collector around.

In exotic cases, usually involving exceptions and scope, there are some edge cases that require one to be a little more careful if the code the does cleanup gets avoided. Realistically, these cases can be designed around. We lived without managed code for many decades.

Often what makes pointers "hard" is merely not understanding what's going on at the hardware level. It's nothing more than indirection.

Pointers give you much more raw access, and this can be very helpful, clever, or necessary. You can point to anywhere and treat it pretty much as anything. If you use your God-like powers for good, it's very, very good.

The con side is usually waste by forgetting to release something, or by releasing it more than once, or referencing something after it's released, or refencing something when you're not pointing anywhere. These things often result in spectacular crashes, and to be honest are usually indicative that you've got a logic problem, rather than pointers are fragile.

If you're a solid developer, using pointers shouldn't be any more problematic than any other data structure. Again, it's not rocket science, and people did it for decades without even blinking an eye. It's just taught less throughly these days.

All that said, unless you have need for pointers, the conveinence and exoitic cases that a good garbage collection provides makes working in a managed environment that much nicer. It's great to be able to grab some memory, use it, and abandon it, knowing that at some time later, it might be discarderd, if it makes sense to do so. That's a bit less code on the coder's part, in exchange for a runtime that does some extra lifting.

share|improve this answer

Pointers provide a visibility into the machine that is required for most interesting programming. Most modern languages simply hide the gritty bits from you.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.