指针,地址,相减结果为什么不是4,而是1?

avici_cpp 2012-12-05 03:56:04

#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?
...全文
177 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
常如意 2012-12-05
  • 打赏
  • 举报
回复
参照谭浩强C语言 230页
mymtom 2012-12-05
  • 打赏
  • 举报
回复
引用 3 楼 avici_cpp 的回复:
道理是这样的. 有官方的、有理有据的解释吗?
看下面的黑体字部分 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.
xumaojun 2012-12-05
  • 打赏
  • 举报
回复
楼主把int指针转换成char指针再减一下试试。
avici_cpp 2012-12-05
  • 打赏
  • 举报
回复
道理是这样的. 有官方的、有理有据的解释吗?
  • 打赏
  • 举报
回复
其实相当于地址相减/sizeof(类型) 用来表示,俩指针间,间隔多少个类型个数。
mujiok2003 2012-12-05
  • 打赏
  • 举报
回复
因为地址相减的结果表示两个地址偏移了多少个元素。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧