views:

361

answers:

5
+2  Q: 

What is a Pointer?

See: Understanding Pointers


In many C flavoured languages, and some older languages like Fortran, one can use Pointers.

As someone who has only really programmed in basic, javascript, and actionscript, can you explain to me what a Pointer is, and what it is most useful for?

Thanks!

+5  A: 

A pointer is a variable that contains the address of another variable. This allows you to reference another variable indirectly. 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
}
Douglas Mayle
This is not totally correct. It could be to a method for example.
mattlant
+8  A: 

This wikipedia article will give you detailed information on what a pointer is:

In computer science, a pointer is a programming language data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address. Obtaining or requesting the value to which a pointer refers is called dereferencing the pointer. A pointer is a simple implementation of the general reference data type (although it is quite different from the facility referred to as a reference in C++). Pointers to data improve performance for repetitive operations such as traversing string and tree structures, and pointers to functions are used for binding methods in Object-oriented programming and run-time linking to dynamic link libraries (DLLs).

David Segonds
A: 

As someone already mention, a pointer is a variable that contains the address of another variable.

It's mosty used when creating new objects (in run-time).

David The Man
Why the down vote?
David The Man
+1  A: 

There have been several discussions in SO about this topic. You can find information about the topic with the links below. There are several other relevant SO discussions on the subject, but I think that these were the most relevant. Search for 'pointers [C++]' in the search window (or 'pointers [c]') and you will get more information as well.

In C++ I Cannot Grasp Pointers and Classes

What is the difference between modern ‘References’ and traditional ‘Pointers’?

Mark
+2  A: 

Pointers are not as hard as they sound. As others have said already, they are variables that hold the address of some other variable. Suppose I wanted to give you directions to my house. I wouldn't give you a picture of my house, or a scale model of my house; I'd just give you the address. You could infer whatever you needed from that.

In the same way, a lot of languages make the distinction between passing by value and passing by reference. Essentially it means will i pass an entire object around every time I need to refer to it? Or, will I just give out it's address so that others can infer what they need?

Most modern languages hide this complexity by figuring out when pointers are useful and optimizing that for you. However, if you know what you're doing, manual pointer management can still be useful in some situations.

JosephStyons