C语言 具有初始值的 C 结构体

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13706809/
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 04:40:37  来源:igfitidea点击:

structs in C with initial values

cstruct

提问by Layla

I want to create a structure in C with initial values like:

我想在 C 中创建一个具有初始值的结构,例如:

typedef struct {
    int id = 0;
    char *name = "none";
} employee;

I know the above is not possible, is there any other way to do this?

我知道以上是不可能的,有没有其他方法可以做到这一点?

回答by MOHAMED

you can not do it in this way

你不能这样做

Use the following instead

请改用以下内容

typedef struct
{
   int id;
   char* name;
}employee;

employee emp = {
.id = 0, 
.name = "none"
};

You can use macro to defineand initializeyour instances. this will make easiier to you each time you want to define new instance and initialize it.

您可以使用宏来定义初始化您的实例。每次你想定义新实例并初始化它时,这会让你更容易。

typedef struct
{
   int id;
   char* name;
}employee;

#define INIT_EMPLOYEE(X) employee X = {.id = 0, .name ="none"}

and in your code when you need to define new instance with employee type, you just call this macro like:

在您的代码中,当您需要使用员工类型定义新实例时,您只需调用这个宏,如:

INIT_EMPLOYEE(emp);

回答by Alok Save

Structure is a data type. You don't give values to a data type. You give values to instances/objects of data types.
So no this is not possible in C.

结构是一种数据类型。您不会为数据类型提供值。您为数据类型的实例/对象赋予值。
所以不,这在 C 中是不可能的。

回答by Donnie

You can do:

你可以做:

struct employee_s {
  int id;
  char* name;
} employee_default = {0, "none"};

typedef struct employee_s employee;

And then you just have to remember to do the default initialization when you declare a new employee variable:

然后你只需要记住在你声明一个新的员工变量时做默认的初始化:

employee foo = employee_default;

Alternatively, you can just always build your employee struct via a factory function.

或者,您可以始终通过工厂函数构建您的员工结构。

回答by Omkant

If you are using gccyou can give designated initializersin object creation.

如果您正在使用,gcc您可以designated initializers创建对象。

typedef struct
{
   int id=0;
   char* name="none";
}employee;

employee e = 
{
 .id = 0;
 .name = "none";
};

Or , simply use like array initialization.

或者,简单地使用像数组初始化。

employee e = {0 , "none"};

employee e = {0 , "none"};

回答by bitmask

You can implement an initialisation function:

您可以实现一个初始化函数:

employee init_employee() {
  empolyee const e = {0,"none"};
  return e;
}

回答by Bogdan Alexandru

Even more so, to add on the existing answers, you may use a macro that hides a struct initializer:

更重要的是,要添加现有答案,您可以使用隐藏结构初始值设定项的宏:

#define DEFAULT_EMPLOYEE { 0, "none" }

Then in your code:

然后在你的代码中:

employee john = DEFAULT_EMPLOYEE;

回答by Maxim Kulkin

You can use combination of C preprocessor functions with varargs, compound literalsand designated initializersfor maximum convenience:

您可以将C 预处理器函数与 varargs复合文字指定的初始值设定项结合使用,获得最大的便利:

typedef struct {
    int id;
    char* name;
} employee;

#define EMPLOYEE(...) ((employee) { .id = 0, .name = "none", ##__VA_ARGS__ })

employee john = EMPLOYEE(.name="John");  // no id initialization
employee jane = EMPLOYEE(.id=5);         // no name initialization

回答by Joseph Pena

You can create a function for it:

您可以为其创建一个函数:

typedef struct {
    int id;
    char name;
} employee;

void set_iv(employee *em);

int main(){
    employee em0; set_iv(&em0);
}

void set_iv(employee *em){
    (*em).id = 0;
    (*em).name = "none";
}

回答by HaxtraZ

If you only use this structure for once, i.e. create a global/static variable, you can remove typedef, and initialized this variable instantly:

如果你只使用这个结构一次,即创建一个全局/静态变量,你可以删除typedef,并立即初始化这个变量:

struct {
    int id;
    char *name;
} employee = {
    .id = 0,
    .name = "none"
};

Then, you can use employeein your code after that.

然后,您可以employee在此之后在您的代码中使用。