C中的指针 [英] Pointers in C
问题描述
我遇到的节目如下
main()
{
int x,y;
int * p1 =& x;
int * p2 =& y;
printf(" ;%d \ n",p1-p2);
}
上述程序的输出为1.
有人可以解释一下它是怎么回事。
提前致谢。
Hi,
I came across a program as follows
main()
{
int x, y ;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", p1-p2);
}
The output of the above program is 1.
Can some one explain me how it is 1.
Thanks in advance.
推荐答案
Praveen写道:
Praveen wrote:
我遇到的程序如下
main()
{
int x,y;
int * p1 =& x;
int * p2 =& ; y;
printf("%d \ n",p1-p2);
}
输出以上程序是1.
有人可以解释一下它是怎么回事。
Hi,
I came across a program as follows
main()
{
int x, y ;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", p1-p2);
}
The output of the above program is 1.
Can some one explain me how it is 1.
因为这就是你的编译器所给出的作为答案,减去两个
无关指针是未定义的行为。
-
Ian Collins。
Because that''s what your compiler gives as the answer, subtracting two
unrelated pointers is undefined behaviour.
--
Ian Collins.
2月21日上午11点18分,Ian Collins< ian- n ... @ hotmail.comwrote:
On Feb 21, 11:18 am, Ian Collins <ian-n...@hotmail.comwrote:
Praveen写道:
Praveen wrote:
Hi,
我遇到的程序如下
main()
{
int x,y;
int * p1 =& x;
int * p2 =& y;
printf(" %d \ n",p1-p2);
}
I came across a program as follows
main()
{
int x, y ;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", p1-p2);
}
上述程序的输出为1。
有人可以解释一下它是怎么回事1.
The output of the above program is 1.
Can some one explain me how it is 1.
>> >因为这是你的编译器给出的答案,减去两个不相关的指针是未定义的行为。
>>>Because that''s what your compiler gives as the answer, subtracting two
unrelated pointers is undefined behaviour.
有些人可以告诉为什么这些不相关吗?
Can Some onr tell why these are unrelated?
>
-
Ian Collins。
>
--
Ian Collins.
拉曼写道:
Raman wrote:
2月21日上午11:18 ,Ian Collins< ian-n ... @ hotmail.comwrote:
On Feb 21, 11:18 am, Ian Collins <ian-n...@hotmail.comwrote:
> Praveen写道:
>Praveen wrote:
Hi,
我遇到的程序如下
main()
{
int x,y;
int * p1 =& x;
int * p2 =& y;
printf("%d \ n",p1-p2);
}
I came across a program as follows
main()
{
int x, y ;
int *p1 = &x;
int *p2 = &y;
printf("%d\n", p1-p2);
}
上述程序的输出是1.
有人可以解释它是怎么回事。
The output of the above program is 1.
Can some one explain me how it is 1.
>> ;>>因为这是你的编译器给出的答案,减去两个
>无关指针是未定义的行为。
>>>>Because that''s what your compiler gives as the answer, subtracting two
>unrelated pointers is undefined behaviour.
一些人可以告诉为什么这些不相关吗?
Can Some onr tell why these are unrelated?
他们不属于同一个对象[1]。 (堆栈帧不是对象,即使在具有堆栈帧的实现中也是
,来自C'的POV。)
[1]其中包含`malloc 结果也是。
-
克里斯电刺猬 Dollin
生活充满了神秘感。考虑其中一个。 Sinclair,/ Babylon 5 /
They''re not part of the same object [1]. (Stack frames are not objects,
even in implementations that have stack frames, from C''s POV.)
[1] Which covers `malloc` results too.
--
Chris "electric hedgehog" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/
这篇关于C中的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!