read the following code:
main()
{
int *p,a[5]={1,2,3,4,5};
for(p=a;p<&a[5];p++)
{
*p=p-a;
printf(“%d “,*p);
}
return 0;
}
theoretical output= 0 4 8 12 16
compiler output=0 1 2 3 4
question: Why p++ (increment in address of p) increments the whole memory block , considering it as a unit and not the individual byte.
It is the property of the pointer.
A pointer to any type is an address in memory which is an integer address. It is not an integer. The association of a pointer to a data type is so that it knows how many bytes the data is stored in. So when we increament a pointer we increase the pointer by one block memory.
if you want this 0 4 8 12 16 as ouput then typecast pointers as chara.
*p = (char*)p-(char*)a;