如何将所有结构字段重置为0或NULL [英] how to reset all structure fields to 0 or NULL

查看:70
本文介绍了如何将所有结构字段重置为0或NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,全部!


我有一个结构:


typedef struct cmd_line_s {

char * cl_action;

char * cl_args [10];

unsigned char cl_args_num;

void * cl_handler;

} cmd_line_t;


在初始化时我正在做cmd_line_t cli = {NULL,NULL,0,NULL};


处理后''cli''我想再次将字段值重置为{NULL,NULL,0,

NULL}状态,最正确和最简单的方法是什么?


谢谢!


最诚挚的问候,罗马马沙克。电子邮件: mr*@tusur.ru

解决方案

Roman Mashak写道:

大家好!

我有一个结构:

typedef struct cmd_line_s {
char * cl_action;
char * cl_args [10];
unsigned char cl_args_num;
void * cl_handler;
} cmd_line_t;

在初始化时我正在做cmd_line_t cli = {NULL,NULL,0,NULL};

处理''cli'后'我想将字段值重置为{NULL,NULL,0,
NULL再次声明,最正确和最简单的方法是什么?




一种方法是从已经初始化的实例中复制:


const cmd_line_t empty_line = {NULL,NULL,0,NULL};

cmd_line_t cli = {NULL,NULL,0,NULL};

...

/ *用cli做事* /

...

cli = empty_line;

/ * cli是现在重新初始化* /


C99提供了另一种方法,但它并不是那么多

在这种用法中比上面更方便。


您显示的初始化程序并不完整,可能会导致某些编译器发出警告消息。您可以尝试将

重写为


{NULL,{NULL},0,NULL}


或即使是
{NULL,

{NULL,NULL,NULL,NULL,NULL,

NULL,NULL,NULL, NULL,NULL},

0,

NULL}


或者也许就像


{0}

-

Eric Sosman
es ***** @ acm-dot-org.inva lid


文章< d8 **** ******@relay.tomsk.ru> ;,罗马马沙克< mr*@tusur.ru>写道:

我有一个结构:
unsigned char cl_args_num;
void * cl_handler;
在处理''cli'之后'我想再次将字段值重置为{NULL,NULL,0,
NULL}状态,最正确和最简单的方法是什么?



保留一个空副本并进行结构分配。之后,

,释放了适当的动态内存。


-

熵是概率的对数 - - Boltzmann


你好,Eric!

你写的是2005年6月13日星期一22:45:55 -0400:


[跳过]

ES>您展示的初始化程序并不完整,可能是

ES>导致某些编译器发出警告消息。您可以尝试

ES>将其重写为


ES> {NULL,{NULL},0,NULL}


ES>甚至作为


ES> {NULL,

ES> {NULL,NULL,NULL,NULL,NULL,

ES> NULL,NULL,NULL,NULL,NULL},

ES> 0,

ES> NULL}


非常感谢!


最诚挚的问候,Roman Mashak。电子邮件: mr*@tusur.ru


Hello, All!

I have a structure:

typedef struct cmd_line_s {
char *cl_action;
char *cl_args[10];
unsigned char cl_args_num;
void *cl_handler;
} cmd_line_t;

At initialization time I''m dooing cmd_line_t cli = { NULL, NULL, 0, NULL };

After dealing with ''cli'' I''d like to reset fields values to { NULL, NULL, 0,
NULL } state again, what is the most correct and easy way?

Thank you!

With best regards, Roman Mashak. E-mail: mr*@tusur.ru

解决方案

Roman Mashak wrote:

Hello, All!

I have a structure:

typedef struct cmd_line_s {
char *cl_action;
char *cl_args[10];
unsigned char cl_args_num;
void *cl_handler;
} cmd_line_t;

At initialization time I''m dooing cmd_line_t cli = { NULL, NULL, 0, NULL };

After dealing with ''cli'' I''d like to reset fields values to { NULL, NULL, 0,
NULL } state again, what is the most correct and easy way?



One way is to copy from an already-initialized instance:

const cmd_line_t empty_line = { NULL, NULL, 0, NULL };
cmd_line_t cli = { NULL, NULL, 0, NULL };
...
/* do things with cli */
...
cli = empty_line;
/* cli is now re-initialized */

C99 provides another way to do this, but it''s not all that much
more convenient than the above in this sort of usage.

The initializer you''ve shown isn''t quite complete, and may
cause some compilers to emit warning messages. You might try
rewriting it as

{ NULL, { NULL }, 0, NULL }

or even as

{ NULL,
{ NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL } ,
0,
NULL }

or perhaps just as

{ 0 }

--
Eric Sosman
es*****@acm-dot-org.invalid


In article <d8**********@relay.tomsk.ru>, Roman Mashak <mr*@tusur.ru> wrote:

I have a structure: unsigned char cl_args_num;
void *cl_handler; After dealing with ''cli'' I''d like to reset fields values to { NULL, NULL, 0,
NULL } state again, what is the most correct and easy way?



Keep a null copy around and do a structure assignment. After, that
is, having freed whatever dynamic memory is appropriate.

--
Entropy is the logarithm of probability -- Boltzmann


Hello, Eric!
You wrote on Mon, 13 Jun 2005 22:45:55 -0400:

[skip]
ES> The initializer you''ve shown isn''t quite complete, and may
ES> cause some compilers to emit warning messages. You might try
ES> rewriting it as

ES> { NULL, { NULL }, 0, NULL }

ES> or even as

ES> { NULL,
ES> { NULL, NULL, NULL, NULL, NULL,
ES> NULL, NULL, NULL, NULL, NULL } ,
ES> 0,
ES> NULL }

Thanks a lot!

With best regards, Roman Mashak. E-mail: mr*@tusur.ru


这篇关于如何将所有结构字段重置为0或NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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