Friday, 24 July 2015

DIFFERENCE BETWEEN POINTER AND ARRAY POINTER

Difference between array and pointer:

  1. Only for pointers, you need to explicitly allocate and deallocate memory, unless pointing to variable. This will be in heap unlike stack as in case for arrays.
  2. For pointer, memory to which it points can be resized using realloc. Array cannot be resized.
  3. You can take address of the pointer variable. Address of array will give you address for first element of that array. This is because array is just a name to a set of memory locations, whereas pointer is a variable.
  4. Sizeof pointer is always fixed. 8 byte for 64 bit machines and 4 bytes for 32 bit machine to store the start address( 64 bit or 32 bit ) of memory. Sizeof array is strlen of array + 1.
  5. array pointer cant be incremented but normal pointer can be increment . ex- int *pt; pt++ valid .  but int ar[100]  arr++ this in invalid ;
  6. c does not allow array names tobe use as an lvalue . hence array name cant be apear on the left hand side of the assigment operator . ie int arr[100]; int *ptr; arr=ptr invalid .
  7. array cant be assign to another array , but point can be assign to another pointer of same type ex- int arr[100],brr[100] arr=brr invalid 
  8.  main difference is the return value of the address operator  address operator return the address of the operator but when address operator is applied to the array name it will give the same value as array referance without the operator i.e int arr[1000] than arr is same as &arr.
  9. last difference is that when we applied sizeof() operator to the array it will give the size of all bytes allocated for the array but for normal pointer it willgive the byte of pointer variable allocation (2 , 4 , 8  dependent on matchine)

No comments:

Post a Comment