Pointer C Notes
Pointer C Notes
Uses Of Pointer:
1> Used to access the array.
2> Used in Dynamic memory allocation.
3> Used to pass arguments by the reference.
IMPORTANT :
1> Pointer always use address of another variable.
2> Pointer has its own address.
3> Pointer can access the value of the variable, whose address is
stored in pointer.
(using dereference * operator).
4> If we increase the ptr without * then pointer get increase by memory block.
means if int ptr is pointing to 1000, if we increase pointer like : ptr++ then it
will point to 1004.
EXAMPLE :
void main()
{
int a= 100 , *ptr;
ptr = &a;
*ptr = 200;
printf("A = %d\t", a);
}
Output :
200
as we have modify the value of a using pointer variable.
___________________________________________________________________________________
Types of Pointer :
A Pointer is used to store the "memory address" of other variable.
1> NULL Pointer :
-pointer that do not point to any memory location.
-they are initialized with NULL value.
Uses :
> It is used when you dont want to pass any valid memory address.
> used in data structure like trees, linked list to indicates the end.
Uses :
this pointer is used in dynamic memory allocation.
As we can typecast the pointer with any type.
so this pointer is used with calloc(), malloc() and realloc().
eg:
int main()
{
int *ptr; //pointer
ptr = (int*)malloc(sizeof(int)); //we have assign the memory
//for single variable.
int a = 17;
ptr = &a; // we have assigned memory address of a int
pointer.
eg = ptr = NULL;
___________________________________________________________________________________
Difference Between : Static Memory Allocation and Static Memory Allocation :
1. malloc():
It is available in header file : stdlib.h
It is refers as the “Memory Allocation” .
This function allocates the single block of requested memory with
default value :
Garbage value.
It is faster than the calloc()
2. calloc():
It is refers as the “Contiguous Memory Allocation”.
This function allocates the separate block for each variable with
default value : 0.
It is slower than malloc().
3. realloc():
It I refers as “Reallocation of a Memory”.
If Memory allocated by the malloc() and calloc() is not sufficient then
user can
Reallocates the memory using realloc() function.
In short it is used to expand the memory.
Application of Pointer :
1. Pointers save memory space.
2. Execution time with pointers is faster because data is directly manipulated with
the address as
Pointer direct access to memory location.
3. Pointers are used with array , string and function.
4. Pointers are used in dynamic memory allocation and file handling to.