简体   繁体   English

指针算术(char *)&a [1] - (char *)&a [0] == 4

[英]pointer arithmetic (char*) &a[1] - (char *)&a[0] == 4

如果a是一个int数组, (char*) &a[1] - (char *)&a[0]等于4,而&a[1] - &a[0]等于1.为什么呢?

Pointer math operates on the size of the data structure its pointing to. 指针数学运算的大小与其指向的数据结构有关。 This is because if I do this: 这是因为如果我这样做:

int array[10] ;
int * p = array ;

p ++ ;

I want p pointing at the second int, not some memory halfway in between two elements. 我希望p指向第二个int,而不是两个元素之间的一些内存。

So &a[1] is four bytes apart from &a[0] but asking it &a[1] - &a[0] asks how many ints apart it is. 所以&a[1]&a[0]相差四个字节,但要求它&a[1] - &a[0]询问它有多少个ints When you cast it to char you ask for the math in terms of the size of char . 当你施放它char你的规模而言要求数学char

When you do 当你这样做

&a[1] - &a[0]

since a is an int array, an implicit (int *) pointer is assumed, that is 因为a是一个int数组,所以假定是一个隐式(int *)指针

(int *)&a[1] - (int *)&a[0]

Hence since both are of type pointer to int , their difference gives 1. 因此,由于两者都是指向int的类型指针,因此它们的差异为1。

But when you do- 但当你这样做 -

(char*) &a[1] - (char *)&a[2]

assuming int is 4 bytes and char is 1 byte on your compiler, the difference will be four since each element of a is int and has four bytes. 假设int是4个字节,char在编译器上是1个字节,差异将是4,因为a的每个元素都是int并且有4个字节。

This is common problem about programmers to treat pointers as just address. 这是关于程序员把指针作为刚刚解决共同的问题。 This is wrong approach. 这是错误的做法。 For the compiler to create proper low level code it must know something more about pointer than just the memory location that it points to. 为了让编译器创建适当的低级代码,它必须知道关于指针的更多信息,而不仅仅是它指向的内存位置。 That additional info is the size of object a pointer is pointing to. 附加信息是指针指向的对象的大小。 That all is contained in pointer type and is necessary for pointer aruthmetic. 这一切都包含在指针类型中,并且对于指针算法是必需的。 For example: 例如:

int a[25] ;
int *i_ptr = a;

printf("address of 1st element %d\n", a);
printf("address of 1st element %d\n", &a[0]);

// address of first element + sizeof(int)
printf("address of 2nd element %d\n", a+1);

// address of first element + sizeof(int)    
printf("address of 2nd element %d\n", &a[1]);

// this one is tricky
// address of first element + sizeof(25*int) ie sizeof(the whole array)
printf("address of JUST AFTER THE ARRAY %d\n", &a+1);

Last is very tricky and most programmers do not even know about it. 最后是非常棘手的,大多数程序员甚至都不知道它。 Although a is pointing to the first element of array and has type of array element &a is something completely different. 虽然a指向数组的第一个元素并且具有数组元素的类型&a是完全不同的东西。 It also keeps addres of array's first element (ie. array start) but it's type is different suggesting compiler that pointer arithmetic on it will be based on array size not element size. 它还保留了数组的第一个元素(即数组启动)的地址,但它的类型不同,这表明编译器上的指针算法将基于数组大小而不是元素大小。

gives: 得到:

address of 1st element 2881388               +0
address of 1st element 2881388               +0
address of 2nd element 2881392               +4
address of 2nd element 2881392               +4
address of JUST AFTER THE ARRAY 2881488    +100

Every time you obtain address of something with & operator it has associated type with it allowing later calculations with that type of pointer. 每次使用运算符获取某些内容的地址时,它都具有与之关联的类型,以便稍后使用该类型的指针进行计算。

Keep in mind that there is a pointer void * that does not have any type info associated with it and you canot perform arithmetic on it. 请记住,有一个指针void *没有与之关联的任何类型信息,您可以对其执行算术运算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM