在memcpy()中强制转换 [英] cast in memcpy()

查看:411
本文介绍了在memcpy()中强制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我对memcpy()的正确用法感到有点困惑。请帮助我

清除混乱。


memcpy(3)的linux手册页给了我以下原型

memcpy (3):

#include< string.h>

void * memcpy(void * dest,const void * src,size_t n);


现在,我有一个char数组(char arr [256])和一个指向结构的指针

(PSMessage * p)。我必须memcpy()这个结构的内容到
char数组。我是这样做的:


memcpy(arr,p,sizeof * p);


有人告诉我 - 这是一个不正确的用法,你要么做

memcpy(arr,(char *)p,sizeof * p);



memcpy( (void *)arr,(void *)p,sizeof * p);


我需要演员吗?或者我试图这样做的方式是正确的吗?

我没有测试过代码,所以我也不确定它。


谢谢你提前。


干杯,

Amarendra


-

生活''战斗并不总是发生在更强壮或更快的人身上。

但是胜利的人迟早会认为自己能胜任。

Hi,

I am a bit confused over the correct usage of memcpy(). Kindly help me
clear the confusion.

The linux manpage for memcpy(3) gives me the following prototype of
memcpy(3):
#include <string.h>
void *memcpy(void *dest, const void *src, size_t n);

Now, I have a char array (char arr[256]), and a pointer to a structure
(PSMessage *p). I have to memcpy() the contents of this structure to
the char array. I am doing it like this:

memcpy(arr, p, sizeof *p);

Someone tells me -- this is an incorrect usage, you either need to do
memcpy(arr, (char *)p, sizeof *p);
or
memcpy((void *)arr, (void *)p, sizeof *p);

Do I need the cast here ? Or the way I am trying to do it is correct ?
I have not tested the code, so I am not sure of it either.

Thanks in advance.

Cheers,
Amarendra

--
Life''s battles don''t always go to the stronger or the faster man,
But sooner or later the man who wins is the man who thinks he can.

推荐答案

ro**@zworg.com (Amarendra GODBOLE)写道:
ro**@zworg.com (Amarendra GODBOLE) writes:
memcpy(arr,p,sizeof * p);

有人告诉我 - 这是一个不正确的用法,你要么需要做
memcpy(arr,( char *)p,sizeof * p);

memcpy((void *)arr,(void *)p,sizeof * p);
memcpy(arr, p, sizeof *p);

Someone tells me -- this is an incorrect usage, you either need to do
memcpy(arr, (char *)p, sizeof *p);
or
memcpy((void *)arr, (void *)p, sizeof *p);




您的线人错了。 C中不需要这样的演员表。

-

int main(void){char p [] =" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz。\

\ n",* q =" kl BIcNBFr.NKEzjwCIxNJC" ;; int i = sizeof p / 2; char * strchr(); int putchar(\

); while(* q){i + = strchr(p,* q ++) - p; if(i> =(int)sizeof p)i- = sizeof p-1; putchar(p [i] \

);}返回0;}



Your informant is wrong. No such casts are necessary in C.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv wxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}


Amarendra GODBOLE写道:
Amarendra GODBOLE wrote:


我有点困惑正确使用memcpy()。请帮助我清除混乱。

memcpy(3)的linux手册页给了我以下原型
memcpy(3):
#include< string.h>
void * memcpy(void * dest,const void * src,size_t n);

现在,我有一个char数组(char arr [256]),和指向结构的指针
(PSMessage * p)。我必须将memcpy()这个结构的内容添加到char数组中。我是这样做的:

memcpy(arr,p,sizeof * p);

有人告诉我 - 这是一个不正确的用法,你要么做
memcpy(arr,(char *)p,sizeof * p);

memcpy((void *)arr,(void *)p,sizeof * p);

我需要演员吗?或者我尝试这样做的方式是正确的?
我没有测试过代码,所以我也不确定。
Hi,

I am a bit confused over the correct usage of memcpy(). Kindly help me
clear the confusion.

The linux manpage for memcpy(3) gives me the following prototype of
memcpy(3):
#include <string.h>
void *memcpy(void *dest, const void *src, size_t n);

Now, I have a char array (char arr[256]), and a pointer to a structure
(PSMessage *p). I have to memcpy() the contents of this structure to
the char array. I am doing it like this:

memcpy(arr, p, sizeof *p);

Someone tells me -- this is an incorrect usage, you either need to do
memcpy(arr, (char *)p, sizeof *p);
or
memcpy((void *)arr, (void *)p, sizeof *p);

Do I need the cast here ? Or the way I am trying to do it is correct ?
I have not tested the code, so I am not sure of it either.




那个& ;有人"是吹烟或者使用C以外的语言。

你的表格没问题。



That "someone" is blowing smoke or else using a language other than C.
Your form is just fine.


On Sun,2004年2月29日20:14 :52 -0800,Ben Pfaff< bl*@cs.stanford.edu>写道:
On Sun, 29 Feb 2004 20:14:52 -0800, Ben Pfaff <bl*@cs.stanford.edu> wrote:
ro**@zworg.com (Amarendra GODBOLE)写道:
ro**@zworg.com (Amarendra GODBOLE) writes:
memcpy(arr,p,sizeof * p);

有人告诉我 - 这是一个不正确的用法,你要么需要做
memcpy( arr,(char *)p,sizeof * p);

memcpy((void *)arr,(void *)p,sizeof * p);
memcpy(arr, p, sizeof *p);

Someone tells me -- this is an incorrect usage, you either need to do
memcpy(arr, (char *)p, sizeof *p);
or
memcpy((void *)arr, (void *)p, sizeof *p);


<你的线人错了。在C中不需要这样的强制转换。



Your informant is wrong. No such casts are necessary in C.




更具体地说,不需要强制转换将任何指针类型转换为

或来自void *。


干杯,

-

格雷格。



To be more specific, no casts are required to convert any pointer type to
or from void *.

Cheers,
--
Greg.


这篇关于在memcpy()中强制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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