4

Conceptually a "pointer" is just something that "points" to something else;Is this definition is sufficient to tell exactly what a pointer is in programing languages? Does it need to have any other features?

Programmers who come from specific language may have pre-conceived ideas of what constitutes a 'pointer' based on how it is used in the language So let say if he is from c/c++ language, he say's pointers supports pointer arithmetic.Is pointer arithmetic an essential feature of a pointer?

Go has pointers and does not support pointer arithmetic. Is the ability to "dereference" the pointer essential to the concept of a pointer?

So what is the precise definition of a pointer that I can give as an answer, irrespective of any specific programming language?

1

5 Answers 5

8

I do not know of any general definition for "pointers", or another related term, "references". It's fairly safe, however, to say that pointers and programming languages supporting pointers usually have several of the following characteristics:

  • At the very least, a pointer is a reference to other objects/values.

  • A pointer type may or may not be typed, i.e. pointers of that type may reference only a certain type of objects/values (e.g. int* vs. void* in C). Whether this characteristic is present or not may depend mostly on the nature of the supporting programming language's type system.

  • A programming language that supports pointers may or may not have a pointer dereference operator, which resolves a pointer to the referenced object/value (e.g. unary * in C).

  • Such a programming language may or may not have an operator that creates a pointer for an object/value (e.g. unary & in C).

  • A pointer may or may not contain a null reference, i.e. not point at anything. (This feature was invented by Tony Hoare, which he now calls his Billion Dollar Mistake.) Such pointers may not be dereferenced, and cannot be the result of the pointer creation operator.

  • A pointer type may or may not support a limited form of arithmetic, e.g. incrementation/decrementation, adding/subtracting an integral value to a pointer value, subtracting two pointers of the same pointer type, etc.

  • A pointer, i.e. a reference to another object, may be implemented using memory addresses (linear, segmented, or otherwise).

5
  • Learned a new thing i.e the Introductor of null reference Thanks for that :)
    – JAVA
    Aug 25, 2013 at 8:37
  • 1
    "adding two pointers of the same pointer type" -> more often subtraction is implemented to give the difference. For example C supports subtraction but not addition. Aug 25, 2013 at 11:14
  • @MaciejPiechotka: Of course you're right. Fixed.
    – stakx
    Aug 25, 2013 at 12:10
  • +1 for knowing the who invented the Null Reference! you learn something new every day
    – Malachi
    Aug 27, 2013 at 18:46
  • A pointer can point to functions (in C) too.
    – ott--
    Aug 27, 2013 at 20:31
2

What is the definition of pointer?

That completely, entirely depends on the language.

Both C and C++ (there's no such language as "C/C++"!) have a solid, precise definition of pointers. For example, C99 says (§6.2.5.20):

A pointer type may be derived from a function type, an object type, or an incomplete type, called the referenced type. A pointer type describes an object whose value provides a reference to an entity of the referenced type. A pointer type derived from the referenced type T is sometimes called ‘‘pointer to T’’. The construction of a pointer type from a referenced type is called ‘‘pointer type derivation’’.

(emphasis mine)

later, when describing the & (address-of) operator (§6.5.3.2.3):

The unary & operator yields the address of its operand. If the operand has type ‘‘type’’, the result has type ‘‘pointer to type’’.

This interpretation again suggests that a "pointer" is simply an address. (Well, not simply, but an address nevertheless.)

Is the ability to "dereference" the pointer essential to the concept of a pointer?

Again, that depends. Conceptually, Java has pointers, but you can't dereference them. Objective-C objects are pointers too, but the compiler whines if you are trying to dereference such a pointer. (I suspect that these phenomena have a common origin - pointers are used in both languages to represent references to objects, and at an implementation level, they are also used for different clever things like polymorphism.)

So what is the precise definition of pointer i can answer irrespective of specific programming language?

Unfortunately, there's no such thing. (Shameless self-promotion: but you can provide a link to this answer if you think it's good enough :P)

1
  • @sunny You are misunderstanding the phrase. The referenced type is any type the pointer points to. It may be a complete or an incomplete type (or a function type).
    – H2CO3
    Aug 25, 2013 at 9:30
1

What a pointer "is" is typically dependent on both the language, and the implementation of that language. e.g: Pointers in C/C++ are typically int's which hold the memory address of the thing pointed at, however they dont necessarily have to be. Thats just how they're usually implemented.

So, thinking about operations that can be performed on a pointer, anything other than a "deference" is really implementation dependent. Being able to do pointer arithmetic is often useful, but depends entirely on the underlying implementation.

Pointers also have a secondary role in c/c++ in that they often point at items allocated on the heap rather than the stack. This is differentiation is often not captured in other languages. Other languages tend to avoid the term "pointer" and use "reference" or "alias" instead.

Personally, I like to think, in a general sense, of pointers as a special type of alias. But when it comes to C/C++ I tend to envisage the entire language as being about memory, and pointers become just one subset of managing memory.

1

A pointer is a kind of variable whose value is a reference to a point in the memory of the machine where the program is running.

Being variables they need to support operations (they would be useless otherwise), the operations supported may vary depending on the programming language.

Many new programming languages (such as Java) even go all the way to hiding pointers to the users (which we could probably say that in these languages pointers are variables supporting no operations).

I would say that any operation on pointers needs to be supported or not depending on the structure of the language. In some way al programming languages use pointers, but, the fact that not all languages give you access to them, is indicative that operations may be necessary or not depending on the structure of the language.

-1

Here's what I learned in CS:

  1. A variable is a named location of a value.

  2. A pointer is a variable whose value is the address of the location of a variable.

And yes, in my opinion pointers in general are dereferencable.

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