printf("%p",& i)不正确吗? [英] Is not printf("%p", &i) correct?

查看:72
本文介绍了printf("%p",& i)不正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第二个参数需要参数类型转换

printf("%p",(void *)& i); ?


但是人们永远不会把memcpy称为:

memcpy((void *)pi,(void *)pj,sizeof * pj);

/ * memcpy((void *)pi,(void *)pj,sizeof * pi); * / / *大小是什么?* /


By那么,memcpy的第三个参数是否会跟随第一个

或第二个参数的大小?如果它遵循第一个参数的大小,则第二个参数指向的内存区域中的
不需要的数据将被复制到第一个参数指向的内存区域。


如果它跟在第二个之后,该函数将写入无效内存

分配给第一个参数的区域之外。

lovecreatesbeauty

Is parameter type conversion required for the 2nd argument on
printf("%p", (void *)&i); ?

But one would never call memcpy like:
memcpy((void *)pi, (void *)pj, sizeof *pj);
/*memcpy((void *)pi, (void *)pj, sizeof *pi);*/ /*size of what?*/

By the way, will the 3rd argument of memcpy follow the size of the 1st
or the 2nd argument? If it follows the size of the 1st argument,
unwanted data in the memory area pointed by the 2nd parameter will be
copied to the memory area pointed by the 1st parameter.

If it follows the 2nd one, the function will write to invalid memory
outside the area allocated to the 1st parameter.

lovecreatesbeauty

推荐答案

lovecreatesbeauty说:
lovecreatesbeauty said:

是否需要参数类型转换

printf("%p",(void *)& i); ?
Is parameter type conversion required for the 2nd argument on
printf("%p", (void *)&i); ?



是的。当

它们是可变参数函数的可变参数部分的参数时,C不提供指针值自动转换为void *,因为它没有办法知道是否这样转换是必要的。

Yes. C does not supply automatic conversion of pointer values to void * when
they are arguments to the variadic part of a variadic function, because it
has no way of knowing whether such conversion is necessary.


但是永远不会像memcpy那样调用memcpy:

memcpy((void *)pi,(void *)pj, sizeof * pj);

/ * memcpy((void *)pi,(void *)pj,sizeof * pi); * / / *大小是什么?* /
But one would never call memcpy like:
memcpy((void *)pi, (void *)pj, sizeof *pj);
/*memcpy((void *)pi, (void *)pj, sizeof *pi);*/ /*size of what?*/



在这种情况下,C知道memcpy为void *,因为它原型为

。但是printf原型为int printf(const char *,...),而

......可以自由地解释为我将不得不信任你。 />
这个东西。换句话说,为/

可变参数函数参数获取类型是'/你/你的工作。

In this case, C knows that memcpy takes void *, because it is prototyped
accordingly. But printf is prototyped as int printf(const char *, ...), and
the ... can be freely interpreted as "I''m going to have to trust you on
this stuff". In other words, it''s /your/ job to get the types right for
variadic function arguments.


顺便说一下, memcpy的第三个参数是否会跟随第一个

或第二个参数的大小?
By the way, will the 3rd argument of memcpy follow the size of the 1st
or the 2nd argument?



memcpy的第三个参数决定了你要复制的字节数。

只有你知道这一点。 C不是。这就是你必须告诉它的原因。

The third argument to memcpy dictates the number of bytes you wish to copy.
Only you know this. C doesn''t. That''s why you have to tell it.


如果它跟随第一个参数的大小,

内存中不需要的数据第二个参数指向的区域将

复制到第一个参数指向的内存区域。
If it follows the size of the 1st argument,
unwanted data in the memory area pointed by the 2nd parameter will be
copied to the memory area pointed by the 1st parameter.



如果你指定合法的字节数并且需要

复制,则不是。

Not if you specify the number of bytes that it is legal and desirable to
copy.


如果它跟在第二个之后,该函数将写入无效内存

分配给第一个参数的区域之外。
If it follows the 2nd one, the function will write to invalid memory
outside the area allocated to the 1st parameter.



如果你指定合法的字节数并且需要

复制,那就没有了。


如果您不知道要复制多少字节,请查看。


-

Richard Heathfield

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

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

Not if you specify the number of bytes that it is legal and desirable to
copy.

If you don''t know how many bytes you want to copy, find out.

--
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)


lovecreatesbeauty写道:
lovecreatesbeauty wrote:

第二个参数需要参数类型转换

printf("%p",(void *)& i); ?
Is parameter type conversion required for the 2nd argument on
printf("%p", (void *)&i); ?



是的,除非你不介意做出不保证的假设

标准C.没有隐含的指针转换在

....具有可变数量参数的函数列表中。

Yes, unless you don''t mind making assumptions that are not guaranteed
by standard C. There are no implicit conversions for pointers in the
.... list of functions with a variable number of arguments.


但是人们永远不会像mem这样调用memcpy: />
memcpy((void *)pi,(void *)pj,sizeof * pj);

/ * memcpy((void *)pi,(void *)pj,sizeof * pi); * / / *大小是什么?* /
But one would never call memcpy like:
memcpy((void *)pi, (void *)pj, sizeof *pj);
/*memcpy((void *)pi, (void *)pj, sizeof *pi);*/ /*size of what?*/



这里,memcpy的原型将确保pi和pj得到转换

to void *和const void *。

Here, the prototype for memcpy will ensure that pi and pj get converted
to void * and const void *.


顺便说一句,memcpy的第3个参数是否会跟随第1个
的大小还是第二个论点?
By the way, will the 3rd argument of memcpy follow the size of the 1st
or the 2nd argument?



如果两个参数的大小相同,则没关系。如果

大小不一样,你有特殊情况需要

自己检查你需要做什么。




Richard Heathfield写道:

Richard Heathfield wrote:

lovecreatesbeauty说:
lovecreatesbeauty said:

第二个参数需要参数类型转换

printf("%p",(void *)& i); ?
Is parameter type conversion required for the 2nd argument on
printf("%p", (void *)&i); ?



是的。当

它们是可变参数函数的可变参数部分的参数时,C不提供指针值自动转换为void *,因为它没有办法知道是否这样转换是必要的。


Yes. C does not supply automatic conversion of pointer values to void * when
they are arguments to the variadic part of a variadic function, because it
has no way of knowing whether such conversion is necessary.



一般指针类型void *之间的转换和其他

指针合法并自动完成并最终执行

,对吗?


C将提供隐含的转换转换指针类型

参数(函数调用)指针类型的参数(声明)

type" void *"没有可变参数列表的地方?


%p转换说明符需要相应的类型值

void *。这可以是%p吗?充当原型的功能并告诉

printf将相应的参数转换为类型void *。


lovecreatesbeauty

The conversion between the general pointer type "void *" and other
pointers is legal and automatically performed completely and performed
eventually, right?

Will C supply implied conversion for converting pointer type of
arguments (function call) to pointer type of parameter (declaration) of
type "void *" where there is not variable argument list?

The "%p" conversion specifier requires a corresponding value of type
void *. Can this "%p" act as the functionality of prototype and tell
the printf to convert the corresponding argument to type "void *".

lovecreatesbeauty


这篇关于printf("%p",& i)不正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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