What is pointer? Explain with examples.
- A pointer is a variable that holds a memory address. This address is the location of another object (typically, a variable) in memory. That is, if one variable contains the address of another variable, the first variable is said to point to the second.
- A pointer declaration consists of a base type, an *, and the variable name.
- The general form of declaring a pointer variable is :
type *name;
-The 'type' is the base type of the pointer and may be any valid type.
- The 'name' is the name of pointer variable.
- The base type of the pointer defines what type of variables the pointer can point to.
- Two special pointer operators are: * and &.
- The & is unary operator that returns the memory address of its operand. It is “the address of” operand.
- The * is complement of &. It is also a unary operator and returns the value located at the address that follows.
int i, *p;
i = 5;
p = &i; //places the memory address of i into p
The expression *p will return the value of variable pointed to by p.