Why does subtracting a pointer in C give an integer?

Why, if I subtract from the pointer, another pointer (integer pointers) without typecasting the result will be 1, not 4 bytes (for example, when I resort to the int method for both pointers). Example:

int a , b , *p , *q; p = &b; q = p + 1; // q = &a; printf("%d",q - p); // The result will be one . printf("%d",(int)q - (int)p); // The result will be 4(bytes). The memory address of b minus The memory address of a. 
+5
source share
5 answers

Why If If I subtract from the pointer, another pointer (integer pointers) without typecasting the result will be 1, not 4 bytes

That the entire data type point is indicated by a pointer. It is probably easier to view the context of the array, as shown below. The point does not depend on the underlying data type (here long or double ), you can use pointer arithmetic to navigate the array without worrying about how exactly the size of its element. In other words, (pointer + 1) means indicate the next element regardless of type.

 long l[] = { 10e4, 10e5, 10e6 }; long *pl = l + 1; // point to the 2nd element in the "long" array. double d[] = { 10e7, 10e8, 10e9 }; double *pd = d + 2; // point to the 3rd element in the "double" array. 

Also pay attention to your code:

 int a , b , *p , *q; p = &b; q = p + 1; // q = &a; <--- NO this is wrong. 

The fact that a and b declared next to each other does not mean that a and b are located next to each other in memory. So q points to the memory address next to address b - but what is in this address is undefined.

+3
source

According to the C standard (6.5.6 additive operators)

9 When two pointers are subtracted, both indicate the elements of the same array object or one after the last element of the array object; the result is the difference in the indices of the two elements of the array ....

If two pointers pointed to elements of the same array, then, as stated in the quote from Standard

the result is the difference in the indices of the two arrays Elements

That is, you get the number of elements in the array between these two pointers. This is the result of so-called pointer arithmetic.

If you subtract the addresses stored in the pointers as integer values, you will get a number corresponding to the arithmetic subtraction operation.

+4
source

Because ptrdiff_t from subtracting the pointer is calculated relative to the size of the marked elements. It is much more convenient; Firstly, it tells you how many times you can increment one pointer before you reach another pointer.

+3
source

where are you

 int a , b , *p , *q; 

The compiler can put a and b anywhere. They do not have to be next to each other. Also, when you subtract two int pointers, the result is int size, not bytes.

+3
source

C is not an assembly language. So pointers are not just simple integers - pointers are special guys who know how to point to other things.

This is fundamental for how pointers and pointer arithmetic work in C, so that they can point to consecutive elements of an array. Therefore, if we write

 int a[10]; int *p1 = &a[4]; int *p2 = &a[3]; 

then p1 - p2 will be 1. The result is 1, because the "distance" between a[3] and a[4] is one int. The result is 1, because 4 - 3 = 1. The result is not 4 (as you might think, it would be if you knew that int is 32 bits on your computer), because we are not interested in doing assembler programming or working with machine addresses; we do higher level language programming with an array, and we think in these terms.

(But, yes, at the address machine level, the method p2 - p1 calculated, as a rule, as (<value of the source address in p2 > - <value of the source address in p1 >)) / sizeof(int) .)

+2
source

Source: https://habr.com/ru/post/1241276/


All Articles