%p和铸造 [英] %p and casting

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

问题描述

你好!


#include< stdio.h>


int main(无效)

{

char * p =" hello,world";


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


返回0;

}


当我用<编译上面的代码时,为什么GCC 3.2没有发出诊断信息? br />
'' - W -Wall -ansi -pedantic -O''? char *如何与int *或double

*不同,这会导致''警告:void格式,不同类型arg(arg 2)''

诊断?<非常感谢

解决方案

" 127.0.0.1" <我们** @ example.net>写道:

#include< stdio.h>

int main(void)
{
char * p =" hello,world" ;

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

返回0;
}

为什么没有'当我使用
'-W -Wall -ansi -pedantic -O''编译上面的代码时,GCC 3.2会发出诊断信息吗? char *如何与int *或double
*不同,这会导致''警告:void格式,不同类型arg(arg 2)''
诊断?




没有要求任何编译器为给定代码生成

的任何诊断。有未定义的行为,但没有约束

违规。


''gcc -W -Wall -ansi -pedantic -O'确实产生了这一事实对于某些指针类型的诊断

而对于其他一些指针类型则没有。

无关紧要和关于comp.lang.c的非主题。


-

Simon。


Simon Biber写道:

" 127.0.0.1" ; <我们** @ example.net>写道:

(...)
char * p =" hello,world";
printf("%p \ n",p);
(...)

当我用''-W -Wall -ansi -pedantic -O'编译上面的代码时,为什么GCC 3.2不会发出诊断信息? ? char *如何与int
*或double *不同,这会导致''警告:void格式,不同类型
arg(arg 2)''诊断?



指向不同类型的指针可以有不同的表示形式。对于

示例,指向''int''的指针可能包含含有该int的

内存位置的''字数'',而指向a的指针''void''可能

包含''字节数''。在这种情况下,要将int *转换为void *,

,int *指针的表示形式必须与int中的

字节数相乘。 br />

在正常的赋值和使用原型的函数调用中,

编译器会为你处理这个问题。有一个例外:参数

传递给具有可变数量参数的函数,比如printf。

在这种情况下,原型并没有说出函数所期望的类型,

所以编译器无法为你转换它。 gcc知道什么是%p格式

的意思,并警告你。

事实上''gcc -W -Wall -ansi -pedantic -O' '确实为某些指针类型生成了一个
诊断,而对于某些其他指针类型则没有。在comp.lang.c上,这些类型是无关紧要和偏离主题的。




坚果。编译器通常有他们所做的事情的理由。在这种情况下,它的原因是关于主题:正如你所说,它是未定义的行为。


-

Hallvard


127.0.0.1写道:

你好!

#include< stdio .h>

int main(无效)
{
char * p =" hello,world" ;;

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

返回0;
}

当我用<编译上面的代码时,为什么GCC 3.2不会发出诊断信息? br />''-W -Wall -ansi -pedantic -O''? char *与int *或
double *有什么不同,导致''警告:void格式,不同类型arg(arg 2)''
诊断?




char *和void *的表示是如此密切相关,以至于

实际上是关于你的程序行为是否为b
的争议明确定义。


为了安全起见,无论如何都要将演员表添加到(void *),就像打印时一样。>
任何对象指针''价值。


-

Richard Heathfield: biwel@eton.powernet.co.uk

Usenet是一个奇怪的地方。 - Dennis M Ritchie,1999年7月29日。

C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

K& R答案,C书等:< a rel =nofollowhref =http://users.powernet.co.uk/etontarget =_ blank> http://users.powernet.co.uk/eton


Hello!

#include <stdio.h>

int main(void)
{
char *p = "hello, world";

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

return 0;
}

Why doesn''t GCC 3.2 issue a diagnostic when I compile the above code with
''-W -Wall -ansi -pedantic -O''? How is char * different from int * or double
*, which cause a ''warning: void format, different type arg (arg 2)''
diagnostic?

Many thanks

解决方案

"127.0.0.1" <us**@example.net> wrote:

#include <stdio.h>

int main(void)
{
char *p = "hello, world";

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

return 0;
}

Why doesn''t GCC 3.2 issue a diagnostic when I compile the above code with
''-W -Wall -ansi -pedantic -O''? How is char * different from int * or double
*, which cause a ''warning: void format, different type arg (arg 2)''
diagnostic?



There is no requirement for any compiler to produce any diagnostic for
the given code. There is undefined behaviour, but no constraint
violation.

The fact that ''gcc -W -Wall -ansi -pedantic -O'' does produce a diagnostic
for some pointer types and does not for some other pointer types is
irrelevant and off-topic on comp.lang.c.

--
Simon.


Simon Biber wrote:

"127.0.0.1" <us**@example.net> wrote:

(...)
char *p = "hello, world";
printf("%p\n", p);
(...)

Why doesn''t GCC 3.2 issue a diagnostic when I compile the above code
with ''-W -Wall -ansi -pedantic -O''? How is char * different from int
* or double *, which cause a ''warning: void format, different type
arg (arg 2)'' diagnostic?


Pointers to different types can have different representations. For
example, a pointer to an ''int'' may contain the ''word number'' for the
memory location containing that int, while a pointer to a ''void'' may
contain its ''byte number''. In that case, to convert an int* to a void*,
the representation of the int* pointer must be multiplied with the
number of bytes in an int.

In normal assignments and in function calls using prototypes, the
compiler takes care of this for you. With one exception: arguments
passed to functions with a variable number of arguments, like printf.
In this case the prototype doesn''t say which type the function expects,
so the compiler can''t convert it for you. gcc knows what the %p format
means, though, and warns you about it.
The fact that ''gcc -W -Wall -ansi -pedantic -O'' does produce a
diagnostic for some pointer types and does not for some other pointer
types is irrelevant and off-topic on comp.lang.c.



Nuts. Compilers usually have reasons for what they do. In this case, its
reason is on-topic: As you say, it''s undefined behaviour.

--
Hallvard


127.0.0.1 wrote:

Hello!

#include <stdio.h>

int main(void)
{
char *p = "hello, world";

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

return 0;
}

Why doesn''t GCC 3.2 issue a diagnostic when I compile the above code with
''-W -Wall -ansi -pedantic -O''? How is char * different from int * or
double *, which cause a ''warning: void format, different type arg (arg 2)''
diagnostic?



The representations of char * and void * are so closely related that there
is actually some dispute as to whether the behaviour of your program is
well-defined.

To be safe, add the cast to (void *) anyway, just as you do when you print
any object pointer''s value.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton


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

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