Why memory address contained by pointer +1 is different from address of value being pointed + 1

爷,独闯天下 提交于 2019-12-24 01:31:52

问题


Pointer stores memory address of value being pointed at so memory address contained by pointer is same as memory address of value. So adding 1 to both these memory addresses should yield same result, which is not happening. Why? Here is the code

 int main()
 {
     int ages[] = {23, 43, 12, 89, 2};
     int *cur_ages = ages;

     printf("\n&cur_ages=%p, &cur_ages+1=%p", &cur_ages, &cur_ages+1);
     printf("\n&ages=%p, &ages+1=%p", &ages, &ages+1);
     printf("\ncur_ages=%p, cur_ages+1=%p", cur_ages, cur_ages+1);
     printf("\n*cur_ages=%d, *cur_ages+1=%d", *cur_ages, *(cur_ages+1));

     return 0;
 }

Output is

&cur_ages=0x1ffff85f3d0, &cur_ages+1=0x1ffff85f3d8
&ages=0x1ffff85f3dc, &ages+1=0x1ffff85f3f0
cur_ages=0x1ffff85f3dc, cur_ages+1=0x1ffff85f3e0
*cur_ages=23, *cur_ages+1=43

&ages+1 is not equal to cur_ages+1. Why?


回答1:


Pointer arithmetic increases the value of a pointer by the given value multiplied by the size of the type it points to.

So when you do this:

&ages+1

You take the address of ages (which is of type int [5]) and add sizeof(ages) to the pointer value. Assuming sizeof(int) is 4, this adds 20 to the pointer value.

Likewise, when you do this:

cur_ages+1

Here, cur_ages points to a int, so adding 1 adds sizeof(int) to the pointer value.




回答2:


&cur_ages is the address where cur_ages is stored on the stack. Since &cur_ages+1 is 8 greater, I would assume this was done in 64 bit mode where the size of a pointer is 8 bytes.

&ages is similar to declaring int (*ptr_to_ages)[5], where ptr_to_ages is a pointer to an array of 5 integers, which could be a pointer to the first row of a matrix where each row is an array of 5 integers. &ages = the address where the first member of the array is stored. As mentioned in dbush's answer, since ages is an array of 5 integers, then &ages+1 points to what would be an array of 5 integers after the 5 integers in ages, so + 5 * 4, where 4 is the size of an integer, effectively the same as a pointer to the second row of a matrix where each row is an array of 5 integers.

The example code doesn't print out ages and ages+1, which would differ by sizeof(int) instead of by 5 * sizeof(int). Without the & prefix on ages, the size of the array would not matter.

cur_ages and cur_ages+1 point to the first and second integers in ages, as expected.

*cur_ages and *(cur_ages+1) are the first and second integer values in ages.




回答3:


Several things are happening here.

  1. &cur_ages is the address of a pointer to a value, not the address of a value. Because of the (arguably odd) semantics of array names in C, &ages is the address of a value.
  2. Pointer arithmetic (&<anything>+1) works by whole items, not by bytes. Therefore, adding 1 to an int * will add sizeof(int) bytes, not one byte.


来源:https://stackoverflow.com/questions/38513618/why-memory-address-contained-by-pointer-1-is-different-from-address-of-value-be

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!