EmbLogic's Blog

output doubt

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.

2 Responses to output doubt

  1. harshvardhan says:

    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.

Leave a Reply to harshvardhan Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>