指针,地址,相减结果为什么不是4,而是1?
#include <stdio.h>
#include <windows.h>
int main(void)
{
int arr[] = {1, 2};
int* p = &arr[0];
int* q = &arr[1];
printf("%#x\n", p);
printf("%#x\n", q);
printf("%#x\n", q-p);
printf("%i\n", q-p);
system("pause");
return 0;
}
这是打印结果:
0x22ff40
0x22ff44
0x1
为啥q-p不是4,而是1?
[解决办法]
因为地址相减的结果表示两个地址偏移了多少个元素。
[解决办法]
其实相当于地址相减/sizeof(类型) 用来表示,俩指针间,间隔多少个类型个数。
[解决办法]
楼主把int指针转换成char指针再减一下试试。
[解决办法]
看下面的黑体字部分
C89
3.3.6 Additive operators
Syntax
additive-expression:
multiplicative-expression
additive-expression + multiplicative-expression
additive-expression - multiplicative-expression
Constraints
For addition, either both operands shall have arithmetic type, or one operand shall be a pointer to an object type and the other shall have integral type. (Incrementing is equivalent to adding 1.)
For subtraction, one of the following shall hold:
* both operands have arithmetic type;
* both operands are pointers to qualified or unqualified versions of compatible object types; or
* the left operand is a pointer to an object type and the right operand has integral type. (Decrementing is equivalent to subtracting 1.)
Semantics
If both operands have arithmetic type, the usual arithmetic conversions are performed on them.
The result of the binary + operator is the sum of the operands.
The result of the binary - operator is the difference resulting from the subtraction of the second operand from the first.
When an expression that has integral type is added to or subtracted from a pointer, the integral value is first multiplied by the size of the object pointed to. The result has the type of the pointer operand. If the pointer operand points to a member of an array object, and the array object is large enough, the result points to a member of the same array object, appropriately offset from the original member. Thus if P points to a member of an array object, the expression P+1 points to the next member of the array object. Unless both the pointer operand and the result point to a member of the same array object, or one past the last member of the array object, the behavior is undefined. Unless both the pointer operand and the result point to a member of the same array object, or the pointer operand points one past the last member of an array object and the result points to a member of the same array object, the behavior is undefined if the result is used as the operand of a unary * operator.
When two pointers to members of the same array object are subtracted, the difference is divided by the size of a member. The result represents the difference of the subscripts of the two array members. The size of the result is implementation-defined, and its type (a signed integral type) is ptrdiff_t defined in the <stddef.h> header. As with any other arithmetic overflow, if the result does not fit in the space provided, the behavior is undefined. If two pointers that do not point to members of the same array object are subtracted, the behavior is undefined. However, if P points either to a member of an array object or one past the last member of an array object, and Q points to the last member of the same array object, the expression (Q+1) - P has the same value as (Q-P) + 1, even though Q+1 does not point to a member of the array object.
[解决办法]
参照谭浩强C语言 230页