In computer science, a pointer is a programming language object, whose value refers to (or "points to") another value stored elsewhere in the computer memory using its memory address.

A pointer references a location in memory, and obtaining the value stored at that location is known as dereferencing the pointer.

As an analogy, a page number in a book's index could be considered a pointer to the corresponding page; dereferencing such a pointer would be done by flipping to the page with the given page number.

Variable and Address

variable: an abstraction of data stored in computer memory.
three attributes:1, name;2, data type; 3, value

One more atribute: address - memory location

address variable – pointer: 1,a kind of variable; 2, contains the address of some other variable.

Declaration
int j;
int *iPtr;

In declarations the asterisk modifier (*) specifies a pointer type. For example, where the specifier int would refer to the integer type, the specifier int* refers to the type "pointer to integer". Pointer values associate two pieces of information: a memory address and a data type. The following line of code declares a pointer-to-integer variable called ptr:

int *ptr;

Referencing

When a non-static pointer is declared, it has an unspecified value associated with it. The address associated with such a pointer must be changed by assignment prior to using it. In the following example, ptr is set so that it points to the data associated with the variable a:

int *ptr;
int a;

ptr = &a;

In order to accomplish this, the "address-of" operator (unary &) is used. It produces the memory location of the data object that follows.

Dereferencing

The pointed-to data can be accessed through a pointer value. In the following example, the integer variable b is set to the value of integer variable a, which is 10:

int *p;
int a, b;

a = 10;
p = &a;
b = *p;

In order to accomplish that task, the unary dereference operator, denoted by an asterisk (*), is used. It returns the data to which its operand—which must be of pointer type—points. Thus, the expression *p denotes the same value as a. Dereferencing a null pointer is illegal.

You have no rights to post comments