A pointer is a variable which contains the address of another variable. If one variable contains address of another variable, first variable is said to point to the second.
Pointer provides a way of accessing variable without referring to the variable directly. It provides a symbolic way to using addresses.
Declaration of pointer variable:
pointers are declared as follows:
datatype *variable_name;
eg. int *p;
In above example *p is a pointer variable of integer type.
There are two special operators used with pointer:
1) * - It returns the value contained in memory location.
2) & - Is a unary operator and returns a memory address of an operand.
eg. p=&a;
In above example, p is a pointer variable which stores address of a.
Pointer notation:
Consider the declaration
int i = 3;
This declaration tells the C Compiler to
a) Reserve space in memory to hold the integer value.
b) Associate the name i with the memory location.
c) Store the value 3 at this location.
We may represent this location in memory by following memory map.
i -> location name
3 -> value at location
Uses of pointer:
1) To return more than one value from the function.
2) To pass arrays and strings more conventionally from one function to another.
3) To manipulate arrays more easily.
4) To allocate memory and access it.
5) For faster execution of program.
6) To create complex data structure such as linked list.
No comments:
Post a Comment