I have done type casting with int and char but not with pointers so I posted this question.
#include <stdio.h>
int main() {
int a[4] = { 1, 2, 6, 4 };
char *p;
p = (char *)a; // what does this statement mean?
printf("%d\n",*p);
p = p + 1;
printf("%d",*p); // after incrementing it gives 0 why?
}
The first call to printf gives the first element of the array. And after p=p+1 it gives 0. Why?
declares a as array.
aat this point of time stores the address of the first element of the array.declares p as a
pointertocharacterNow since
p&aboth stores addresses. So the address stored ata(addressof first element of the array) is assigned top. The typecasting is done aspwas declared aschar *what it does is that, assuming address stored at
ais say 100 and assuming thatinttakes 2 bytes andchartakes 1 bytes in memoryand
and that will explain the output as
A. two bytes stored at
acontains the first element of the array.B. one byte stored at
pis the character representation of the first byte of integer1, which is0.Hope this helps.