Pointer vs Array

Let you teach what is difference between pointer and array. Firs of all we should know the definition of pointer and array.

Pointer:
Pointer is a variable that capable of storing the address
Array:
Array is collection of same elements



Difference between Pointer and Array:
Array size is fixed and can't be expand at run time. Pointer can be expand at run time using the malloc function.

Pointer memory is allocated using this code:
ptr=(int*)malloc(size);
size defines bytes required for the pointer.

consider this eg:
int *ptr;
ptr=(int *)malloc(sizeof(int)*10);//allocates 20 bytes(2byte for int).
ptr=realloc(20,40);//reallocates the memory to 40 bytes.

Related Posts