同一指针变量的值不同。 [英] Different value of same pointer variable.
问题描述
#include< stdio.h>
void main(无效)
{
int * p,* q;
p = q + 200;
printf("%d%d%d%d",p,q,(int)p,(int)q );
}
结果是,400 1439 0 1439。
我不喜欢理解p和(int)p显示不同的结果。
请帮帮我。
#include <stdio.h>
void main ( void )
{
int *p, *q;
p = q + 200;
printf("%d %d %d %d", p, q, (int)p, (int)q);
}
The result is, "400 1439 0 1439".
I don''t understand "p" and "(int)p" show different result.
Please help me.
推荐答案
persevere写道:
persevere wrote:
#include< stdio.h>
void main(void)
{* / int * p,* q;
p = q + 200;
printf("%d%d%d%d",p,q,(int)p,(int)q);
}
结果是,400 1439 0 1439。
我不明白p和(int)p显示不同的结果。
请帮帮我。
#include <stdio.h>
void main ( void )
{
int *p, *q;
p = q + 200;
printf("%d %d %d %d", p, q, (int)p, (int)q);
}
The result is, "400 1439 0 1439".
I don''t understand "p" and "(int)p" show different result.
Please help me.
也许是因为这不是C ++程序?尝试:
1)int main,而不是void main
2)reinterpret_cast< int>(p),而不是(int)p
此外,< iostream>通常优于< stdio.h>
希望这会有所帮助,
-shez-
Maybe because this is not a C++ program? Try:
1) int main, instead of void main
2) reinterpret_cast<int>(p), instead of (int)p
Also, <iostream> is usually better than <stdio.h>
Hope this helps,
-shez-
persevere写道:
persevere wrote:
#include< stdio.h>
void main(void)
{* / * int * p,* q;
p = q + 200;
printf("%d%d%d%d",p,q,(int )p,(int)q);
}
结果是,400 1439 0 1439。
我不明白p"和(int)p显示不同的结果。
请帮帮我。
#include <stdio.h>
void main ( void )
{
int *p, *q;
p = q + 200;
printf("%d %d %d %d", p, q, (int)p, (int)q);
}
The result is, "400 1439 0 1439".
I don''t understand "p" and "(int)p" show different result.
Please help me.
你在计算p时使用未初始化的q。
指针的有效值为0,已分配内存,超过分配的内存块。
其他任何内容都会导致UB。
我编译了你的代码并收到警告:
c:\ MeasureString\copycon\copycon.cpp(9):警告C4311:''type cast'':
指针截断从''int *''到''int''
c:\ MeasureString\copycon\copycon.cpp(9):警告C4311:''type cast'' :
指针截断从''int *''到''int''
c:\ MeasureString \ copycon \ copycon.cpp(9):警告C4313 :''printf'':
''%d''格式字符串与类型''int *''的参数1冲突
c:\ MeasureString \ copycon\copycon.cpp(9):警告C4313:''printf'':
''%d''格式字符串与类型''int *''
c:\measurestring\copycon \\ copycon.cpp(8):警告C4700:局部变量
''q''未经初始化使用
/ dan
you are using an uninitialized q when calculating p. Valid values for
pointers are 0, allocated memory, one past the allocated memory block.
Anything else leads to UB.
I compiled your code and I get warnings:
c:\MeasureString\copycon\copycon.cpp(9) : warning C4311: ''type cast'' :
pointer truncation from ''int *'' to ''int''
c:\MeasureString\copycon\copycon.cpp(9) : warning C4311: ''type cast'' :
pointer truncation from ''int *'' to ''int''
c:\MeasureString\copycon\copycon.cpp(9) : warning C4313: ''printf'' :
''%d'' in format string conflicts with argument 1 of type ''int *''
c:\MeasureString\copycon\copycon.cpp(9) : warning C4313: ''printf'' :
''%d'' in format string conflicts with argument 2 of type ''int *''
c:\measurestring\copycon\copycon.cpp(8) : warning C4700: local variable
''q'' used without having been initialized
/dan
persevere写道:
persevere wrote:
#include< stdio.h>
void main(void)
{* / * int * p,* q;
p = q + 200;
printf("%d%d%d%d",p,q,(int )p,(int)q);
}
结果是,400 1439 0 1439。
我不明白p"和(int)p显示不同的结果。
因为你的程序有很多未定义的行为。
1)main()返回int。总是
2)你从一个未初始化的指针开始
3)你对不指向同一个对象的指针进行算术运算
4)你使用错误的格式化标志输出指针
请帮帮我。
#include <stdio.h>
void main ( void )
{
int *p, *q;
p = q + 200;
printf("%d %d %d %d", p, q, (int)p, (int)q);
}
The result is, "400 1439 0 1439".
I don''t understand "p" and "(int)p" show different result.
Because your program has lots of undefined behaviour.
1) main() returns int. Always
2) You start with a non initialized pointer
3) You do arithmetic to a pointer which do not point to the same object
4) You use the wrong formatting flag for outputting pointers
Please help me.
从上面的3中,第2点和第4点是认真的足够的
任何事情都可能发生。第3点是一个问题,但在大多数系统上通常可以忽略
(至少对于这样的例子)
-
Karl Heinz Buchegger
kb******@gascad.at
这篇关于同一指针变量的值不同。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!