what is the main difference between arrays and pointers in c?
By balabrahmam
@balabrahmam (1071)
India
2 responses
@dholey (1383)
• India
11 Jan 07
it is a nice question, i will try to explain it,
in c , array name stands for the base addres of that array, that is , if you mention only the array name you will get the address of very first element (zeroth element), so it seems similar, but arrays are created with numbers of bytes (sum of the bytes required by members) for example
if you create array of 5 int (int ar[5]) then the array will be created using 10 bytes,
and pointers are special variables which deals with addresses, i.e. the value of pointer variable will be the address of some other variable, so in this case if you use array name or pointer name they seems similar as both will give addresses, but pointer is created using 2 bytes only (as memory address can be stored in two bytes)
poinetr is a variable so the value of pointer can be changed , i.e the address stored in pointer can be changed where in array it always gives the base address , it can not be changed
array notations are converted into pointer notation, so pointers can be used as array notations
when we use ar[1] =10;
it is converted as *(ar+1) =10;
and pointer arithmetic methodes are used to solve the equation
so pointer can also be used with array notation,
so the basic behavior of pointer and array are similar, (taht's why we create run time array using pointer)
p = (int *) malloc(5 * sizeof(int));
then we can use p[1].. p[2]... as well as *(p+1) *(p+2) too
so the conclusion is
1. pointer is a variable , where array name is fixed
2. pointer takes 2 bytes where array is created as mentioned earlier
3. array notation are converted in pointer notations,
4. both stands for addresses.
5. array is group of variables where pointer is one variable
1 person likes this