结构比较/复制 [英] Struct compares/copies

查看:58
本文介绍了结构比较/复制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问:我需要复制和比较C结构。


这是安全快捷的方法吗?:


void func(void)

{

typedef _MY_STRUCT

{

int a;

char b;

char c;

} my_struct


my_stuct new;

my_struct旧的;


if(memcmp(old,new,sizeof(new))

{

memcpy(旧的,新的, sizeof(new));

}

}

Q: I need to copy and compare C Structs.

Is this the safe and quick way to do it?:

void func(void)
{
typedef _MY_STRUCT
{
int a;
char b;
char c;
} my_struct

my_stuct new;
my_struct old;

if (memcmp(old,new,sizeof(new))
{
memcpy(old, new, sizeof(new));
}
}

推荐答案

6月16日星期六2007年14:01:25 GMT, bb@c.co.uk 写道:
On Sat, 16 Jun 2007 14:01:25 GMT, bb@c.co.uk wrote:

>问:我需要复制和比较C Structs。

这是安全快捷的方法吗?:

void func(void)
{
typedef struct _MY_STRUCT
{
int a;
char b;
char c;
} my_struct;

my_stuct new;
my_struct old;

if(memcmp(old,new,sizeof(new))
{
memcpy(ol d,new,sizeof(new));
}
}
>Q: I need to copy and compare C Structs.

Is this the safe and quick way to do it?:

void func(void)
{
typedef struct _MY_STRUCT
{
int a;
char b;
char c;
} my_struct;

my_stuct new;
my_struct old;

if (memcmp(old,new,sizeof(new))
{
memcpy(old, new, sizeof(new));
}
}



拼写错误!

typos corrected!

6月16日下午7:01,b ... @c.co.uk写道:
On Jun 16, 7:01 pm, b...@c.co.uk wrote:

问:我需要复制和比较C结构。


这是安全快捷的方法吗?:


void func(void)

{

typedef _MY_STRUCT

{

int a;

char b;

char c;


} my_struct


my_stuct new;

my_struct old;


if(memcmp(old,new,sizeof(new))

{

memcpy(old,new,sizeof(new));

}


} - 隐藏引用的文字 -


- 显示引用的文字 -
Q: I need to copy and compare C Structs.

Is this the safe and quick way to do it?:

void func(void)
{
typedef _MY_STRUCT
{
int a;
char b;
char c;

} my_struct

my_stuct new;
my_struct old;

if (memcmp(old,new,sizeof(new))
{
memcpy(old, new, sizeof(new));
}

}- Hide quoted text -

- Show quoted text -



我认为在这段代码中使用memcmp就好了,

if(!memcmp(old,new,sizeof(new))

{

memcpy(old,new,sizeof(new));

}


如果内容相同,为什么你需要再次复制呢?

对我而言,代码还可以。


2007年6月16日星期六14:12:48 -0000,deepak< de ********* @ gmail.com>

写道:
On Sat, 16 Jun 2007 14:12:48 -0000, deepak <de*********@gmail.com>
wrote:

> 6月16日下午7:01,b ... @c.co.uk写道:
>On Jun 16, 7:01 pm, b...@c.co.uk wrote:

>问:我需要复制并比较C Structs。

这是安全快捷的方法吗?:

void func(void)
{
typedef _MY_STRUCT
{
int a;
char b;
char c;

} my_struct

my_stuct new;
my_struct old;

if(memcmp(old,new,sizeof(new))
{
memcpy(旧的,新的,sizeof(新的));
}

} - 隐藏引用的文字 -

- 显示引用的文字 -
>Q: I need to copy and compare C Structs.

Is this the safe and quick way to do it?:

void func(void)
{
typedef _MY_STRUCT
{
int a;
char b;
char c;

} my_struct

my_stuct new;
my_struct old;

if (memcmp(old,new,sizeof(new))
{
memcpy(old, new, sizeof(new));
}

}- Hide quoted text -

- Show quoted text -


我认为在这段代码中使用memcmp就好了,如果(!memcmp(old,new,sizeof) (新))
{

memcpy(旧的,新的,sizeof(新的));


如果内容相同,为什么你需要再次复制?


I think the use of memcmp in this code will be like,
if (!memcmp(old,new,sizeof(new))
{
memcpy(old, new, sizeof(new));
}

If the content is same why you need to copy it again?



对不起,是的,如果匹配,memcmp会返回0。错字(另一个)。

Sorry, yes memcmp returns 0 if match. Typo (another one).


>对我而言,代码还可以。
>Else for me code is ok.



Sweet。谢谢。

Sweet. Thank-you.


这篇关于结构比较/复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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