Why array is different from pointer explain with example?

Answer: – Bothe array and pointer allocate memory previously before storage data in memory location both arrays and pointers are used to store large amount of same data in memory location.

Arrays are used in C programming for static memory allocation. It allocate memory at compile time. Its size is always fix because memory allocated at compile time and programmer cannot increase or decrease size of array during the run time. Insertion deletion and updating data in array is very complicated because data are store at their predefine location. When array is created, memory allocate serially starting from zero and up to n-1 term. And in that case, insertion, updating and deletion is very complicated task for the programmer. A major drawback of an array that is if less amount of data stared in a declared array, remaining spaces are unused because these spaces are already reserved by array variable. Another drawback of an array variable is that no more data can be store in memory against it’s predefine size because memory is allocated at compile time. An example of an array is.

#include<stdio.h>
#include<conio.h>
void main()
{
                int i,n[10];
                clrscr();
                for(i=0;i<10;i++)
                                scanf("%d",&n[i]);
                for(i=0;i<10;i++)
                                printf("%d ",n[i]);
                getch();
}

Here in above example, variable n is declared as an array variable. Its dimension also declared as 10. That means up to 10 integer values can be store in this array variable. At run time when user enter values for variable n, first value will be stored in n[0] subscript, second value will be stored in n[1] subscript and so on up to n[9] subscript. During declaration of array, its index number starting from zero and continue up to nine.

Pointer, on the other hand, also used in c programing for memory allocation. It allocate memory dynamically and its size is flexible during the run time it increase as well as decrease memory allocation dynamically. Pointer in C serve two purpose. It’s one purpose is to copy address of other variable and its other purpose to store data dynamically in memory location. Pointer provide multiple facility for the user which is not available in array. Various data structure such as linked list, tree, graph and so on uses pointer variable for memory allocation dynamically. An example of pointer is.

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct link
{
                int data;
                struct link *next;
}*h,*e,*f,*p;
void main()
{
                clrscr();
                h=(struct link*)malloc(sizeof(struct link));
                scanf("%d",&h->data);
                h->next=NULL;
                e=h;
                while(1)
                                {
                                f=(struct link*)malloc(sizeof(struct link));
                                scanf("%d",&f->data);
                                f->next=NULL;
                                e->next=f;
                                e=f;
                                if(f->data==0)
                                                break;
                                }
                while(1)
                                {
                                printf("%d ",h->data);
                                h=h->next;
                                if(h->data==0)
                                                break;
                                }
                getch();
}

Here in case of above example there is no limitation enter data in memory because it is dynamically allocated updating, insertion and deletion are also easy and strait forward method in pointer instead of in array.

Advertisements