开发者

Clarfication on pointer arithmetic

开发者 https://www.devze.com 2023-02-13 02:19 出处:网络
*(*(p+a)+b) If a*size is added to an address (p), then why is b*size added to *(p+a)? *(p+a) appears to b开发者_StackOverflow社区e the value at that location and adding b*size to it would change its
*(*(p+a)+b)

If a*size is added to an address (p), then why is b*size added to *(p+a)? *(p+a) appears to b开发者_StackOverflow社区e the value at that location and adding b*size to it would change its value, not the address. However, based on what I read this is meant to be added to an address.


Given your expression, and assuming you're talking about C, p must be a pointer to a pointer. What happens is:

char ** p;
int a = 2;
int b = 4;

(p+a) // adds 2 * sizeof(char *)
*(p+a) // evaluates to a char *
char * c = *(p+a)
(c+b) // adds 4 * sizeof(char)
*(c+b) // evaluates to a char
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号