What are Pointers in C
It is a variable which is used to store the address of another variable.
Advantages of pointer
- Return multiple values from function
- Access any memory location
- Improves the performance
- Reduces the code
- Used for dynamic memory allocation
- Used in arrays, functions and structures
||address of operator
||Determines the address of a variable.
||Accesses the value at the address.
int *p; //declaration of pointer
p=&n; //stores the address of number variable
printf("Address of n variable is %x \n",&n);
printf("Address of p variable is %x \n",p);
printf("Value of p variable is %d \n",*p);
Address of n variable is fff4
Address of p variable is fff4
Value of p variable is 50
Dynamic memory allocation
Dynamic memory allocation means to allocate the memory at run time. Dynamic memory allocation is possible by 4 functions of stdlib.h header file.
||Allocates single block of requested memory.
||Allocates multiple block of requested memory.
||Reallocates the memory occupied by malloc() or calloc() functions.
||Frees the dynamically allocated memory.