![]() |
Pointers in C Language |
PointersPointer is a derived data type in c language. It can store the address of memory where program instructions or data are stored.
For example:- if int a=5; then pointer stored the address of "int a" (ptr= &a).
Note: In C language "&" is an address operators.
Note: In C language "&" is an address operators.
Importance of pointers
- Pointers can be used to return multiple values.
- Pointers allow to manage the dynamic memory.
- Pointers provide efficient tool for manipulating dynamic data structure.
Declaration of pointers
data_type *ptr_name;Styles
- Int* ptr;
- Int *ptr;
- Int * ptr;
Int quantity;
Int *ptr; // declaration
ptr = &quantity; // initialisation
Program:
#include<stdio.h>
int main()
{
int a = 5;
int *ptr;
ptr = &a;
printf("%u",ptr);return 0;
}
Chain of pointers
A pointer can also stored the address of other pointer.Rule of pointer
There are following rules:
- A pointer variables must be assigned the address of another variable.
- A pointer variable can be initialized with NULL or zero.
- An integer value may be added or subtracted from a pointer variable.
Array pointers
When the pointer is store the address of an array.Program:
#include<stdio.h>
int main()
{
int arr[5] = {1,2,3,4,5};
int *ptr;
ptr = &arr;
printf("%u",ptr);return 0;}