Introduction to pointer
Pointers are as important part of c language. Pointer provide a powerful and flexible way to manipulate data. To understand pointer we need to understand how computer main memory RAM is organized.
Memory organization :
The computers main memory RAM consist of a large number of sequential storage locations. Each memory location is identified by unique address. The address are numbered sequential for 0 to some maximum number depending upon the memory size. i. e they are positive integer. When we run a program the program code and program data. Occupy some part of the main memory. When we use a variable in a program the compiler sets a side a memory location for that variable. It associates the address with the variable name.
WHAT IS POINTER ?
A pointer is a variable that stores the address of the another variable. i.e it points to the another variable.
APPLICATIONS OF POINTER:
1.The pointer are used to pass the parameter by reference. i.e. the actual argument can be modified.
2. All array are implemented as a pointer in c language.
3. They are used for passing array and string to the function.
4. Pointer are used for dynamic memory allocation where memory is allocated and released for a variable during run time.
5. All file handling operation are performed using pointer.
6. They are used in complex data structure like linked list ,tree ,graph etc.
THE BASIC OF POINTER:
1. PONTER DECLARATION:
Since the pointer is a variable it has to declare like any other variable before it can be used.
Syntax:
1. data_type is in c data type. It indicates the type of the variable that the pointer points to.
2. The asterisk(*) is indirection operator.
3. Pointer name is valid C identifire.
2. PONTER DEFINATTION :
The pointer must be assigned the address of the variable before it can be used. The address of the variable is obtained by the address of operator(&) The pointer can be defined by a statement of the form.
Syntax:
E.G: ptr=&n;
assign address of variable n to ptr.
3. POINTER INITIALIZATION:
Assigning an address to a pointer during declaration is called initialization.
Syntax:
E.g: int *ptr=&n
4. PONTER DEREFERENCING:
To use a pointer it has to declare dereferenced. De-referencing operation accesses the data in the memory address which the operator holds. The Dereferenced operatoris * (indirection operator). It is also called the value at the operator.
syntax :
Example:
int n=20,*ptr;
ptr = &n;
printf("%d",*ptr) // display 20
POINTER ARITHMETICS:
The c language allows the five Arithmetics operations to be performed on pointers.
1. Increment ++
2. Decrement --
3. Addition +
4. Subtraction -
5. Differencing
1.Increment or Decrement:
when a pointer incremented using ++, The address in the pointer is increment by a sizeof (data_type) the new value to the pointer will be
(current address in the pointer) + sizeof(data_type))
Incrementing a pointer to an int will cause its value to be incremented by sizeof(int).
similarly a pointer to a float will be incremented by sizeof(float).
The same concept is apply for decrementing the pointer i.e if a pointer is decremented it is decremented by sizeof(data_type).
2. Addition and subtraction:
C allows integer to be added to or subtracted for pointer. When a pointer is incremented by an integer i.e. the new value will be
(current address in pointer ) + i* sizeof(data)type))
Example: int *ptr,n = 20;
ptr = &n;
ptr= ptr+3;
This code will increment the address in the ptr by 3*sizeof(int). If the size of (int) is 2 then the pointer will increment by 6.
Subtraction is performed in the same way.
3. DIFFERENCING:
Differencing is the subtraction of two pointer. The subtraction of two pointer gives the number of element between the two pointer. The result which an integer which is ;
Address in pointer1 - address in poinnter2 / sizeof(data_type)
Example:
int a[5] = {10,20,30,40,50};
int *p,*q;
p = &a[0];
q= &a[3];
printf("%d",q-p);
The output of q-p will be (1006-1000)/sizeof(int). i.e 6/2 = 3.
RELATIONSHIP BETWEEN ARRAY AND POINTER:
Array and pointer are very closely related. All array are implemented as pointer in 'C'. The array name is a constant pointer and it is stores th base address of the array.
int a[5];
here a is an array name and it is also a pointer. It stores the base address of the array i.e &a[0]
An array operation is converted to corresponding pointer operation. The C compiler internally convert the subscripted notation a[i] to the form *(a+i).
a[i] = *(a+i)
Dynamic memory allocation :
When variable are declared in a program, the compiler calculates the size of variable and allocates the memory to the variable. This method is called static memory allocation. The amount of memory required is calculated during compile time. In case of array the size of the array has to be declared at the beginning. In many cases a user does not know how many element are to be put. In such a case memory is either wasted if the size is specified is very large or enough memory is not allocated if the size specified is smaller than required.
In the dynamic memory allocation method we can allocate and de-allocate memory whenever required. Dynamic memory allocation means allocating memory at run-time.
C-provide the four memory allocation and de-allocation functions. They are declared in stdlib.h.
1. malloc:- Allocates requested the number of bytes and returns the pointer to the first byte.
2. calloc:-This also allocates memory for a group of objects, initializes them zero and returns a pointer to the first byte.
3. realloc:-It changes the size of previously allocated block of memory and returns the pointer to the block.
4. free:-Release or free previously allocated space.
ALLOCATION:
Two functions are used to allocates the block of memory dynamically:
These are:
1. malloc allocation :- The allocation function is used to allocate memory aa single block of memory specified size. The malloc() function allocates the size number of bytes of
storage and returns the a pointer to the first byte. It returns NULL if allocation is unsuccessful or if size is 0.
The prototype of malloc is:
Usage: pointer = (type *)malloc(number_of_bytes);
2. calloc allocation : The calloc function() is similar to malloc. It allocates a single memory block of the specified size and also initialize the byte is 0.
The Prototype is :
Difference between malloc and calloc:
1. malloc require one argument i.e total number of bytes to be allocated. calloc requires the two arguments- the number of objects and size of each object.
2. Memory allocates by malloc contain garbage value. i.e malloc does not initialize the memory whereas calloc() initialize the memory to 0.
3. Reallocation :
Reallocation means the changing the size of allocated block of memory. The realloc() function is used to reallocate memory for a block which was dynamically allocated memory using malloc() or calloc().
Prototype of reallocation:
ptr points to the original block, size is the required new size in bytes:
1. If ptr is NULL realloc acts like malloc and returns a pointer to it.
2. If argument size is 0, the memory that the ptr points to is freed and function returns NULL.
3. If sufficient space exits to expand memory block, additional memory is allocated and function returns ptr.
4. If sufficient space does not exist to expand the current block, a new block of the same size is allocated existing data coppied in to it. old block is freed and
a pointer to the new block is returned.
5. If memory is insufficient for reallocation , the function returns the NULL and the old block remains unchanged.
5.Freeing or De-allocating memory:
The dynamically allocated memory is taken from the dynamic memory pool(heap) that is avialble to the program. After the program finishes using that memory block, it should be freed to make memory available for future use. The free() function is used to release the memory that was allocated by malloc() or calloc() or realloc().
Prototype of free allocation:
Memory leak and dangling pointer:
Memory leak:
Memory leak occures when there is dynamically allocated memory area in a heap but no pointer vairable pointing to the memory. A memory leak is memory which hasn't been freed and there is no way to access it. This usually happens when a pointer which was the only reeference to the dynamically allocated memory location points somewhere else now.
Example:
int *ptr1 = (int *)malloc(10);
int * ptr2 = (int *)malloc(20);
ptr1 = ptr2;
here the first block of 10 bytes does not have any pointer pointing to it anymore. Hence it is a memory leak.
Dangling pointer:
When there is a pointer variable which holds the address of a memory block but there is no memory in heap, the pointer is dangling pointer. A dangling pointer is the reverse of the memory leak. When you free an area of memory but still keep a pointer to it that the pointer is dangling.
Example: int *ptr = (int *)malloc(10);
printf("Address in ptr = %u",ptr);
free(ptr);
printf("Address in ptr =%u",ptr);
ptr = NULL;
TYPES OF PPOINTER IN C :
1. NULL POINTER: A NULL is a pointer which is pointing to nothing. Usually pointers are initialized in the begining to NULL indicates the it does not have any valid address.
2. DANGLING POINTER: A pointer pointing to a memory location that has been deleted is called dangling pointer.
3. GENERIC POINTER: When a pointer is declared as type void it is known as a generic pointer. this pointer can not be used directly to points to any element i.e it can not be de- referenced. This pointer can be typecast to any other pointer type. Hence it is called the generic pointer.
4. WILD POINTER: A pointer which has not been initialized to anything is known as a wild pointer. A wild pointer points to some random memory location.
5. NEAR POINTER : A near pointer can stores the 16 bit address which can points to only 64KB data segments or segment number 8 is known as near pointer. It can not access the data beyond the data segment to access data like graphics video memory, text video memory, etc. size of near pointer is two bytes.
6. FAR POINTER : A pointer which can point or access the while memory if RAM i.e all 16 segment is known as fat pointer. Size of far pointer is 4 bytes or 32 bit. In case of far pointer a segment is fixed.
7. HUGE POINTER : A pointer which can point or access the whole the residense memory of RAM i.e. which can access all 16 segments is known as huge pointer. Size ofof the far pointer is 4 bytes or 32 bit. In huge pointer the segment part can be modified.
8.COMPLEX POINTER : Multilevel pointer, pointer to function, pointer to array function, pointer to multidimensional array, pointer in nested in structure or union etc. are complex pointers.