结构初始化C / C ++编程语言?

时间:2022-09-06 11:18:57

I could do struct initialization with code:

我可以使用代码进行struct初始化:

struct struct_type_id struct_name_id = { value1, value2, value3 };

but could not with:

但不能用:

struct struct_type_id struct_name_id;
struct_name_id = { value1, value2, value3 };

why I could do it with the former,but could not with the latter with gcc,g++,vc2008,vc6?In other words,why the c/c++ programming language do not support this syntax?

为什么我能用前者做到这一点,但后者不能用gcc,g ++,vc2008,vc6?换句话说,为什么c / c ++编程语言不支持这种语法?

thanks.

5 个解决方案

#1


22  

The first statement creates a variable initialized to the given values, i.e., these values are built in memory and stored directly in the program executable in that variable address (for globals) or ready for memory copy (for stack variables).

第一个语句创建一个初始化为给定值的变量,即这些值构建在内存中并直接存储在该变量地址中的程序可执行文件中(对于全局变量)或准备好进行内存复制(对于堆栈变量)。

The second statement of the second block is very different. Although it looks similar, it is an assign expression. It means that the RHS of the equals operator is an expression that is evaluated (independently of what is in the LHS of =), and then passed to the = operator. Without proper context, {...} doesn't have any meaning.

第二个块的第二个声明非常不同。虽然它看起来很相似,但它是一个赋值表达式。这意味着equals运算符的RHS是一个被计算的表达式(独立于LHS中的=),然后传递给=运算符。没有适当的背景,{......}没有任何意义。

In C99, you can do this:

在C99中,您可以这样做:

struct_name_id = (struct struct_type_id){ value1, value2, value3 };

Now the RHS of the equals operator is a valid expression, since there is proper context for the compiler to know what is in {...}.

现在,equals运算符的RHS是一个有效的表达式,因为编译器有适当的上下文来知道{...}中的内容。

In C++11, the syntax is:

在C ++ 11中,语法是:

struct_name_id = struct_type_id{ value1, value2, value3 };

#2


4  

I don't know why C didn't originally support some kind of syntax to 'reinitialize' a struct using something like the initializer list - there are definitely times when I would have found it handy. As Juliano mentioned, C99 (and C++0x) has fixed it to a certain degree, but I often have to stick with C90. When I want to do something like that, I'll sometimes do the following:

我不知道为什么C最初不支持某种语法来使用像初始化列表这样的东西来“重新初始化”一个结构 - 肯定有时候我会发现它很方便。正如Juliano所提到的,C99(和C ++ 0x)已将其修复到一定程度,但我经常不得不坚持使用C90。当我想做类似的事情时,我有时会做以下事情:

struct foo const init_foo = { 1, 2, 3};
struct foo myFoo;

// ....

myFoo = init_foo;  // reinitialize myFoo

#3


2  

You just need to cast the values as such:

你只需要像这样强制转换值:

struct_name_id = (struct struct_type_id){ value1, value2, value3 };

#4


0  

I faced a similar problem, and the solution to that was that I was trying to initialized the struct outside the function(not using the initializer syntax, but with the obj.member = VALUE; notation). It is a related problem, so posting here, hoping someone with the same question lands up here.

我遇到了类似的问题,解决方案是我试图在函数外部初始化结构(不使用初始化器语法,但使用obj.member = VALUE;表示法)。这是一个相关的问题,所以发布在这里,希望有同样问题的人登陆这里。

#5


0  

Will this work for you ?

这对你有用吗?

typedef struct name_id {int value1; int value2; int value3;} NAME_ID;
name_id mynameid = {0,1,2};

#1


22  

The first statement creates a variable initialized to the given values, i.e., these values are built in memory and stored directly in the program executable in that variable address (for globals) or ready for memory copy (for stack variables).

第一个语句创建一个初始化为给定值的变量,即这些值构建在内存中并直接存储在该变量地址中的程序可执行文件中(对于全局变量)或准备好进行内存复制(对于堆栈变量)。

The second statement of the second block is very different. Although it looks similar, it is an assign expression. It means that the RHS of the equals operator is an expression that is evaluated (independently of what is in the LHS of =), and then passed to the = operator. Without proper context, {...} doesn't have any meaning.

第二个块的第二个声明非常不同。虽然它看起来很相似,但它是一个赋值表达式。这意味着equals运算符的RHS是一个被计算的表达式(独立于LHS中的=),然后传递给=运算符。没有适当的背景,{......}没有任何意义。

In C99, you can do this:

在C99中,您可以这样做:

struct_name_id = (struct struct_type_id){ value1, value2, value3 };

Now the RHS of the equals operator is a valid expression, since there is proper context for the compiler to know what is in {...}.

现在,equals运算符的RHS是一个有效的表达式,因为编译器有适当的上下文来知道{...}中的内容。

In C++11, the syntax is:

在C ++ 11中,语法是:

struct_name_id = struct_type_id{ value1, value2, value3 };

#2


4  

I don't know why C didn't originally support some kind of syntax to 'reinitialize' a struct using something like the initializer list - there are definitely times when I would have found it handy. As Juliano mentioned, C99 (and C++0x) has fixed it to a certain degree, but I often have to stick with C90. When I want to do something like that, I'll sometimes do the following:

我不知道为什么C最初不支持某种语法来使用像初始化列表这样的东西来“重新初始化”一个结构 - 肯定有时候我会发现它很方便。正如Juliano所提到的,C99(和C ++ 0x)已将其修复到一定程度,但我经常不得不坚持使用C90。当我想做类似的事情时,我有时会做以下事情:

struct foo const init_foo = { 1, 2, 3};
struct foo myFoo;

// ....

myFoo = init_foo;  // reinitialize myFoo

#3


2  

You just need to cast the values as such:

你只需要像这样强制转换值:

struct_name_id = (struct struct_type_id){ value1, value2, value3 };

#4


0  

I faced a similar problem, and the solution to that was that I was trying to initialized the struct outside the function(not using the initializer syntax, but with the obj.member = VALUE; notation). It is a related problem, so posting here, hoping someone with the same question lands up here.

我遇到了类似的问题,解决方案是我试图在函数外部初始化结构(不使用初始化器语法,但使用obj.member = VALUE;表示法)。这是一个相关的问题,所以发布在这里,希望有同样问题的人登陆这里。

#5


0  

Will this work for you ?

这对你有用吗?

typedef struct name_id {int value1; int value2; int value3;} NAME_ID;
name_id mynameid = {0,1,2};