int main()
{
int a=3000,b=20,sum;
char *p;
p=(char *)a;
sum= (int)&p[b]; //adding a & b
printf("%d",sum);
return 0;
}
How this code is working than start from the statement p=(char *)a;
Here you are assigning the value of a to p means p will be pointing at location address equivalent to p
So for the above example
p=3000
Now sum=(int )&p[b];
For this case here p[b] is equivalent to (p+b) because you are using char pointer as p if it is other pointer type than it will be equivalent to (p+(size of data type) * b)
So the statement becomes like this
Sum=(int)&(p+b)
Because p is pointer so you need & to assign it to a variable and int is used to change the value into integer.
So you get a+b in the sum.
No comments:
Post a Comment