<i id='g32C7'><tr id='g32C7'><dt id='g32C7'><q id='g32C7'><span id='g32C7'><b id='g32C7'><form id='g32C7'><ins id='g32C7'></ins><ul id='g32C7'></ul><sub id='g32C7'></sub></form><legend id='g32C7'></legend><bdo id='g32C7'><pre id='g32C7'><center id='g32C7'></center></pre></bdo></b><th id='g32C7'></th></span></q></dt></tr></i><div id='g32C7'><tfoot id='g32C7'></tfoot><dl id='g32C7'><fieldset id='g32C7'></fieldset></dl></div>

      <small id='g32C7'></small><noframes id='g32C7'>

    1. <tfoot id='g32C7'></tfoot>
      • <bdo id='g32C7'></bdo><ul id='g32C7'></ul>

      <legend id='g32C7'><style id='g32C7'><dir id='g32C7'><q id='g32C7'></q></dir></style></legend>
    2. C/C++ 编程语言的结构初始化?

      Struct initialization of the C/C++ programming language?(C/C++ 编程语言的结构初始化?)

        <tbody id='Wm3fy'></tbody>

        <small id='Wm3fy'></small><noframes id='Wm3fy'>

      • <legend id='Wm3fy'><style id='Wm3fy'><dir id='Wm3fy'><q id='Wm3fy'></q></dir></style></legend>

          <tfoot id='Wm3fy'></tfoot>
        1. <i id='Wm3fy'><tr id='Wm3fy'><dt id='Wm3fy'><q id='Wm3fy'><span id='Wm3fy'><b id='Wm3fy'><form id='Wm3fy'><ins id='Wm3fy'></ins><ul id='Wm3fy'></ul><sub id='Wm3fy'></sub></form><legend id='Wm3fy'></legend><bdo id='Wm3fy'><pre id='Wm3fy'><center id='Wm3fy'></center></pre></bdo></b><th id='Wm3fy'></th></span></q></dt></tr></i><div id='Wm3fy'><tfoot id='Wm3fy'></tfoot><dl id='Wm3fy'><fieldset id='Wm3fy'></fieldset></dl></div>

            • <bdo id='Wm3fy'></bdo><ul id='Wm3fy'></ul>
              • 本文介绍了C/C++ 编程语言的结构初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我可以用代码进行结构初始化:

                I could do struct initialization with code:

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

                但不能:

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

                为什么我用前者可以,而用gcc、g++、vc2008、vc6就不能用后者?换句话说,为什么c/c++编程语言不支持这种语法?

                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?

                谢谢.

                推荐答案

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

                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).

                第二块的第二条语句很不一样.虽然看起来很像,但它是一个赋值表达式.这意味着等号运算符的 RHS 是一个表达式,它被评估(独立于 = 的 LHS 中的内容),然后传递给 = 运算符.没有适当的上下文,{...} 没有任何意义.

                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.

                在 C99 中,您可以这样做:

                In C99, you can do this:

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

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

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

                在 C++11 中,语法是:

                In C++11, the syntax is:

                struct_name_id = struct_type_id{ value1, value2, value3 };
                

                这篇关于C/C++ 编程语言的结构初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to print vector#39;s data(如何打印矢量的数据)
                Visual C++ appends 0xCC (int3) bytes at the end of functions(Visual C++ 在函数末尾附加 0xCC (int3) 字节)
                How to use a variable inside a _T wrapper?(如何在 _T 包装器中使用变量?)
                MSVC++ warning flags(MSVC++ 警告标志)
                How to read file which contains uxxxx in vc++(如何在vc++中读取包含uxxxx的文件)
                stack overflow error in C++ program(C++程序中的堆栈溢出错误)

                    • <bdo id='IvFHT'></bdo><ul id='IvFHT'></ul>
                    • <i id='IvFHT'><tr id='IvFHT'><dt id='IvFHT'><q id='IvFHT'><span id='IvFHT'><b id='IvFHT'><form id='IvFHT'><ins id='IvFHT'></ins><ul id='IvFHT'></ul><sub id='IvFHT'></sub></form><legend id='IvFHT'></legend><bdo id='IvFHT'><pre id='IvFHT'><center id='IvFHT'></center></pre></bdo></b><th id='IvFHT'></th></span></q></dt></tr></i><div id='IvFHT'><tfoot id='IvFHT'></tfoot><dl id='IvFHT'><fieldset id='IvFHT'></fieldset></dl></div>
                      • <tfoot id='IvFHT'></tfoot>
                            <tbody id='IvFHT'></tbody>

                          <legend id='IvFHT'><style id='IvFHT'><dir id='IvFHT'><q id='IvFHT'></q></dir></style></legend>

                          <small id='IvFHT'></small><noframes id='IvFHT'>