DIFFERENCE BETWEEN ARRAY AND POINTER - C-Tutorial

Latest

Saturday, 29 October 2016

DIFFERENCE BETWEEN ARRAY AND POINTER


DIFFERENCE BETWEEN ARRAY AND POINTER 

ARRAY
POINTER

1. Array is a collection of homogeneous data elements that are stored in successive memory locations.
1. Pointer is a variable that holds address of another variable.
2. The general form of array declaration is :
Syntax    :   Datatype ArrayName[size];
Example :  int x[10];
2. The general form of a pointer variable declaration is :
Syntax    :   Datatype *PtrVaribale;
Example :  int *ptr;
3. The general form of initializing a array is:
Syntax   :
Datatype Array[size]={List of values};

Example: int x[4] = {2,5,1,7};
3. The general form of initializing a pointer variable is:
Syntax   :   PtrVariable = &Variable;

Example:  int x, *p;
                  p = &x;
4. For an array, compiler allocates memory space based on the size of the array.
Example:   int x[5];

Here, compiler allocates 10 bytes of memory for the variable x.
4. For any type pointer variables compiler allocates only 2 bytes of memory.

Example:  int *p;     char *q;

For p compiler allocates 2 Bytes and for q also compiler allocates 2 Bytes memory.
5. sizeof(arrayname) gives the number of bytes of the memory occupied by the array.
5. sizeof(pointervaraible) returns only 2 bytes.
6. For arrays only static memory allocation is possible.
6. For pointer variables dynamic memory allocation is possible.
7. Arrays can’t be resized.
7. Pointer allocation can be resized using realloc() function.
8. It cannot be reassigned.
8. It can be reassigned.
9. /* Example Program */

#include<stdio.h>
#include<conio.h>
main()
{
     int x[5]={2,4,6,1,3},i;
     clrscr();
     printf(“\nArray Elements Are=”);
     for(i=0;i<5;i++)
     printf(“    %d”,x[i]);
}

9. /* Example program for Pointer */

#include<stdio.h>
#include<conio.h>
main()
{
    int x = 10, *p=&x;
    clrscr();
    printf(“\nResult = %d”,*p);
}

No comments:

Post a Comment