日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

c语言结构体中整形数组初始化,c – 将{0,0}在结构体中初始化数组?

發布時間:2023/12/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c语言结构体中整形数组初始化,c – 将{0,0}在结构体中初始化数组? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

A C = {0,0};

8.5.1聚合[dcl.init.aggr] / 12

Braces can be elided in an initializer-list as follows. If the

initializer-list begins with a left brace,then the succeeding

comma-separated list of initializer-clauses initializes the members of

a subaggregate; it is erroneous for there to be more

initializer-clauses than members. If,however,the initializer-list

for a sub- aggregate does not begin with a left brace,then only

enough initializer-clauses from the list are taken to initialize the

members of the subaggregate; any remaining initializer-clauses are

left to initialize the next member of the aggregate of which the

current subaggregate is a member.

[Example:

06001

is a completely-braced initialization: 1,3,and 5 initialize the first row of the array y[0],namely y[0][0],

y[0][1],and y[0][2]. Likewise the next two lines initialize y[1] and

y[2]. The initializer ends early and therefore y[3]’s elements are

initialized as if explicitly initialized with an expression of the

form float(),that is,are initialized with 0.0. In the following

example,braces in the initializer-list are elided; however the

initializer-list has the same effect as the completely-braced

initializer-list of the above example,

06002

The initializer for y begins with a left brace,but the one for y[0] does not,therefore three elements

from the list are used. Likewise the next three are taken successively

for y[1] and y[2]. — end example ]

下一個

8.5.1聚合[dcl.init.aggr] / 7

If there are fewer initializer-clauses in the list than there are

members in the aggregate,then each member not explicitly initialized

shall be initialized from its brace-or-equal-initializer or,if there

is no brace-or-equal- initializer,from an empty initializer list.

在你的情況下,這意味著第一個0被分配給B [0],第二個0分配給B [1].然后根據8.5.1 / 7,其余的元素是值初始化的.

但是,為了清楚這種情況,您應該使用A C = {{0},0};或者更好

A C{}; // or A C = {};

唯一讓我擔心的是g警告(-Wextra):

warning: missing initializer for member ‘main()::A::D’

[-Wmissing-field-initializers] A C {0,0};

但是根據我對上述標準的解釋,你應該是OK,D應該被初始化.我甚至測試了一些新的布局,結果是如預期的

#include

int main()

{

struct A { int B[100]; int D;};

A memory{};

memory.D = 42;

std::cout << memory.D << std::endl;

// let's place something an A at the location of memory

A* foo = new (&memory) A{0,0};

// line below outputs 0,so D is erased; not the case if A* foo = new (&memory) A;

std::cout << memory.D << std::endl; // outputs 0

}

總結

以上是生活随笔為你收集整理的c语言结构体中整形数组初始化,c – 将{0,0}在结构体中初始化数组?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。