int * p = 7 [英] int *p=7

查看:140
本文介绍了int * p = 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int * p = 3;


int main()

{

int * p = 0;

printf("%x \ n",p);

返回0;

}

打印0在我测试的所有平台上或我在printf

设置断点然后在int * p = 0之后打印

p的值,我得到0.


行为是根据
定义的实现 http://www.vmunix.com/~gabor/c/draft.html#6.2.2.3


有人能告诉为什么它的实现定义和它在哪里它b $ b不同,

不能说int * p = 0或int * p = 3之后的p值为0或者3

解决方案

ni **** *@gmail.com 说:


int * p = 3;


int main()< br $>
{

int * p = 0;

printf("%x \ n && ,p);

返回0;

}



foo.c:1:警告:初始化使得指针来自整数而没有强制转换

foo.c:4:警告:函数声明不是原型

foo.c:在函数`main''中:

foo.c:5:警告:声明'p''阴影全局声明

foo.c:6:警告:隐式声明函数`printf''

foo.c:6:警告:unsigned int格式,指针arg(arg 2)


该程序包含至少三个严重问题,由

上面的第一,第四和第五个警告。


第三个警告回答你的实际问题。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)




Richard Heathfield写道:

ni*****@gmail.com 说:


int * p = 3;


int main()

{

int * p = 0;

printf("%x \ n",p);

返回0;

}



foo.c:1:警告:初始化使得整数指针没有强制转换

foo.c:4:警告:函数声明不是原型

foo.c:函数`main'':

foo.c:5:警告:'p''阴影的声明全局声明

foo.c:6:警告:隐式声明函数`printf''

foo.c:6:警告:unsigned int格式,指针a rg(arg 2)


该程序包含至少三个严重问题,由上面的第一,第四和第五个警告表示

。 />

第三个警告回答你的实际问题。



#include< stdio.h>

int

main()

{

int * p = 0;

printf("%x \ n",p);

返回0;

}

i得到警告tt.c:6:警告:unsigned int格式,指针arg(arg

2)

#include< stdio.h>

int

main()

{

int * p = 0;

printf("%p \ n",p);

返回0;

}

i得到警告tt.c:6:警告:无效格式,不同类型arg(arg

2)


#include< stdio.h>

int

main()

{

int * p = 0;

printf("%p\ n",(void *)p);

返回0;

}


没有警告我得到0,我原来的回答我不明白。

你能不能理解它。理查德。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)


6任何指针类型都可能被转换为整数类型。除了先前指定的

之外,

结果是实现定义的。如果结果不能用整数类型表示

,那么行为是未定义的。结果不必在任何整数

类型的值范围内。


ISO / IEC 9899:1999( E)


也许你可以实现无法用32位表示的指针

整数,尽管几乎所有的实现都允许。


int *p=3;

int main()
{
int *p=0;
printf("%x\n",p);
return 0;
}
prints 0 on all the platforms i test or i set a breakpoint at printf
and then print the value of
p just after int *p=0, i get 0.

the behaviour is implementation defined according to
http://www.vmunix.com/~gabor/c/draft.html#6.2.2.3

Can someone tell me why it s implementation defined and where it
differs ,
can''t i say value of p after int *p=0 or int *p=3 is 0 or 3

解决方案

ni*****@gmail.com said:

int *p=3;

int main()
{
int *p=0;
printf("%x\n",p);
return 0;
}

foo.c:1: warning: initialization makes pointer from integer without a cast
foo.c:4: warning: function declaration isn''t a prototype
foo.c: In function `main'':
foo.c:5: warning: declaration of `p'' shadows global declaration
foo.c:6: warning: implicit declaration of function `printf''
foo.c:6: warning: unsigned int format, pointer arg (arg 2)

The program contains at least three serious problems, which are indicated by
the first, fourth, and fifth warnings above.

The third warning answers your actual question.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)



Richard Heathfield wrote:

ni*****@gmail.com said:

int *p=3;

int main()
{
int *p=0;
printf("%x\n",p);
return 0;
}


foo.c:1: warning: initialization makes pointer from integer without a cast
foo.c:4: warning: function declaration isn''t a prototype
foo.c: In function `main'':
foo.c:5: warning: declaration of `p'' shadows global declaration
foo.c:6: warning: implicit declaration of function `printf''
foo.c:6: warning: unsigned int format, pointer arg (arg 2)

The program contains at least three serious problems, which are indicated by
the first, fourth, and fifth warnings above.

The third warning answers your actual question.

#include<stdio.h>
int
main ()
{
int *p =0;
printf ("%x\n", p);
return 0;
}
i get a warning tt.c:6: warning: unsigned int format, pointer arg (arg
2)
#include<stdio.h>
int
main ()
{
int *p =0;
printf ("%p\n", p);
return 0;
}
i get a warning tt.c:6: warning: void format, different type arg (arg
2)

#include<stdio.h>
int
main ()
{
int *p =0;
printf ("%p\n", (void *)p);
return 0;
}

no warning i get 0 , i the original answer i don''t understand clearly.
Can you throw some light on it Richard.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


6 Any pointer type may be converted to an integer type. Except as
previously specified, the
result is implementation-defined. If the result cannot be represented
in the integer type,
the behavior is undefined. The result need not be in the range of
values of any integer
type.

ISO/IEC 9899:1999 (E)

Maybe you can implement pointer that cannot be represented in 32-bit
integer, although almost all implementations allows.


这篇关于int * p = 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆