Friday, October 16, 2009

Pointers

A pointer is a variable whose value is used to point to another variable. The general form of a pointer declaration is
data-type *pointer-name;
Here data-type specifies the type of data to which the pointer points. pointer-name is the name of the pointer variable,
which can be any valid variable name in C. When the compiler sees the asterisk (*) prefixed to the variable name in
the declaration, it makes a note in its symbol table so that the variable can be used as a pointer.
Usually, the address associated with a variable name is called the left value of the variable. When a variable is
assigned with a value, the value is stored in the reserved memory location of the variable as the content. The content
is also called the right value of the variable.
A pointer is said to be a null pointer when its right value is 0. Remember that a null pointer can never point to valid
data.
The dereference operator (*) is a unary operator that requires only one operand. For instance, the *ptr_name
expression returns the value pointed to by the pointer variable ptr_name, where ptr_name can be any valid variable
name in C.
The & operator is called the address-of operator because it can return the address (that is, left value) of a variable.
Several pointers can point to the same location of a variable in the memory. In C, you can move the position of a
pointer by adding or subtracting integers to or from the pointer.
Note that for pointers of different data types, the integers added to or subtracted from the pointers have different
scalar sizes.

No comments:

Post a Comment