Pointers: Pointers are one of the derived types in C. some of the advantages of pointers are
given below:
• Pointers provide direct access to memory
• Pointers provide a way to return more than one value to the functions
• Pointers provide efficient techniques for manipulating data in arrays
• Pointers can be used to pass information back and forth between the calling function and
called function
• Pointers allows us to perform dynamic memory allocation and deallocation
• Pointers help us to build complex data structures like linked list, stack, queues, trees, and
graphs etc.
Indirection operator (*): It is called as „Value at address‟ operator. It returns the value stored at
a particular address. It is also known as Dereferencing Operator
Address operator (&): The address operator (&) extracts the address for a variable. The address
operator format is as follows. &variablename;
Ex: to know the address of the variable n, just use &n
The format specifier of address is %u(unsigned integer). We can also use %x to know the
address of a variable.
The & operator can be used only with a simple variable or with an element of the array. If x is an
array then expressions such as &x[0], &x[i+3] are valid and represent the addresses of 0th and
(i+3) th elements of x.
Pointer variable: A variable which holds the address of some other variable is called pointer
variable. A pointer variable should always contain the address only.
Declaring a pointer variable In C, every variable must be declared before they are used. Since
the pointer variables contain address that belongs to a separate data type, they must be declared
as pointers before we use them.
The syntax for declaring a pointer variable is as follows
data type *ptr_name;
For example, int *p; declares the variable p as a pointer variable that points to an integer data
type. Remember that the type int refers to the data type of the variable being pointed by p
PSC VVIT, CSE Dept Page 1
,UNIT IV Pointers
Initializing Pointers: Once a pointer variable has been declared, it can be made to point to a
variable using statement such as
ptr_name=&var;
The above statement causes ptr_name to point to var. Now ptr_name contains the address of
var. This is known as pointer initialization. Before a pointer is initialized it should not be used.
Access the value of a variable using pointer variable: Once a pointer variable has been
assigned the address of a variable, we can access the value of a variable using the pointer. This is
done by using the indirection operator(*) by writing as
*ptr_name
1) Meaning of following simple pointer declaration and definition:
int a=5;
int * ptr;
ptr=&a;
Explanation:
About variable a:
1. Name of variable: a
2. Value of variable which it keeps: 5
3. Address where it has stored in memory: 1025 (assume)
About variable ptr:
4. Name of variable: ptr
5. Value of variable which it keeps: 1025
6. Address where it has stored in memory: 5000 (assume)
Pictorial representation:
PSC VVIT, CSE Dept Page 2
, UNIT IV Pointers
Pointer arithmetic or Address arithmetic: The following are the operations that are possible
with the pointers.
1. A pointer can be assigned to another pointer. This will cause both the pointers point to the
same object.
2. A pointer can be incremented or decremented. Each time a pointer is incremented it points to
the memory location of the next element of the base type. Each time a pointer is decremented
it points to the memory location of the previous element of the base type.
3. An integer can be added to or subtracted from a pointer.
4. One pointer can be subtracted from another pointer provided that they are pointing to the
same array. The expression returns the number of elements between them.
PSC VVIT, CSE Dept Page 3