C语言 将结构初始化/重置为零/空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6891720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:16:36  来源:igfitidea点击:

Initialize/reset struct to zero/null

cstructinitialization

提问by hari

struct x {
    char a[10];
    char b[20];
    int i;
    char *c;
    char *d[10];
};

I am filling this struct and then using the values. On the next iteration, I want to reset all the fields to 0or nullbefore I start reusing it.

我正在填充这个结构,然后使用这些值。在下一次迭代中,我想将所有字段重置为0null在我开始重用它之前。

How can I do that? Can I use memsetor I have to go through all the members and then do it individually?

我怎样才能做到这一点?我可以使用memset还是必须通过所有成员然后单独进行?

回答by David Heffernan

Define a const static instance of the struct with the initial values and then simply assign this value to your variable whenever you want to reset it.

使用初始值定义结构的 const 静态实例,然后只要您想重置它,只需将此值分配给您的变量。

For example:

例如:

static const struct x EmptyStruct;

Here I am relying on static initializationto set my initial values, but you could use a struct initializer if you want different initial values.

在这里,我依靠静态初始化来设置我的初始值,但如果您想要不同的初始值,您可以使用结构初始化程序。

Then, each time round the loop you can write:

然后,每次循环时,您都可以编写:

myStructVariable = EmptyStruct;

回答by Jens Gustedt

The way to do such a thing when you have modern C (C99) is to use a compound literal.

当您拥有现代 C (C99) 时,执行此操作的方法是使用复合文字.

a = (const struct x){ 0 };

This is somewhat similar to David's solution, only that you don't have to worry to declare an the empty structure or whether to declare it static. If you use the constas I did, the compiler is free to allocate the compound literal statically in read-only storage if appropriate.

这有点类似于 David 的解决方案,只是您不必担心声明空结构或是否声明它static。如果您const像我一样使用 ,编译器可以在适当的情况下自由地在只读存储中静态分配复合文字。

回答by user411313

Better than all above is ever to use Standard C specification for struct initialization:

比以上所有更好的是使用标准 C 规范进行结构初始化:

struct StructType structVar = {0};

Here are all bits zero (ever).

这是所有位为零(永远)。

回答by templatetypedef

In C, it is a common idiom to zero out the memory for a structusing memset:

在 C 中,将structusing的内存清零是一种常见的习惯用法memset

struct x myStruct;
memset(&myStruct, 0, sizeof(myStruct));

Technically speaking, I don't believe that this is portable because it assumes that the NULLpointer on a machine is represented by the integer value 0, but it's used widely because on most machines this is the case.

从技术上讲,我不认为这是可移植的,因为它假定NULL机器上的指针由整数值 0 表示,但它被广泛使用,因为在大多数机器上都是这种情况。

If you move from C to C++, be careful not to use this technique on every object. C++ only makes this legal on objects with no member functions and no inheritance.

如果您从 C 迁移到 C++,请注意不要在每个对象上使用此技术。C++ 只在没有成员函数和继承的对象上使这合法。

回答by Rudy Velthuis

If you have a C99 compliant compiler, you can use

如果你有一个 C99 兼容的编译器,你可以使用

mystruct = (struct x){0};

otherwise you should do what David Heffernan wrote, i.e. declare:

否则你应该做大卫赫弗南写的,即声明:

struct x empty = {0};

And in the loop:

在循环中:

mystruct = empty;

回答by Diego Sevilla

You can use memsetwith the size of the struct:

您可以使用memset结构的大小:

struct x x_instance;
memset (&x_instance, 0, sizeof(x_instance));

回答by Archmede

I believe you can just assign the empty set ({}) to your variable.

我相信您可以将空集 ( {})分配给您的变量。

struct x instance;

for(i = 0; i < n; i++) {
    instance = {};
    /* Do Calculations */
}

回答by Josh Matthews

 struct x myX;
 ...
 memset(&x, 0, sizeof(myX));

回答by gatis paeglis

Or if you want to have an API in your struct for that you can do:

或者,如果你想在你的结构中有一个 API,你可以这样做:

struct Register {
    ..
    ..
    void reset() { *this = {}; }
    ..
}

so later you can do this:

所以稍后你可以这样做:

Register reg;
// do some work
reg.reset()