在结构初始化中初始化结构?
这可能听起来有点愚蠢,但我必须知道,因为我正在用 C 编写宾果游戏板。
#include <stdio.h>
typedef struct {
int a;
int b;
int c;
int d;
int e;
} row;
typedef struct {
row one;
row two;
row three;
row four;
row five;
} bingo_board;
void initialize_columns()
{
bingo_board board = {
.one = {1, 2, 3, 4, 5},
.two = {6, 7, 8, 9, 10},
.three = {11, 12, 13, 14, 15},
.four = {16, 17, 18, 19, 20},
.five = {21, 22, 23, 24, 25}
};
}
这可能吗?
This may sound somewhat stupid, but I have to know as I'm writing a bingo board in C.
#include <stdio.h>
typedef struct {
int a;
int b;
int c;
int d;
int e;
} row;
typedef struct {
row one;
row two;
row three;
row four;
row five;
} bingo_board;
void initialize_columns()
{
bingo_board board = {
.one = {1, 2, 3, 4, 5},
.two = {6, 7, 8, 9, 10},
.three = {11, 12, 13, 14, 15},
.four = {16, 17, 18, 19, 20},
.five = {21, 22, 23, 24, 25}
};
}
Is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它可以简单地完成
或 甚至
不需要“标记”每一行。不过,标记语法在 C99 中可用,并且示例中的内容对于 C99 来说已经是正确的。
It can be done simply as
Or even as
No need to "tag" every row. The tagged syntax is available in C99 though, and what you have in your example is already correct for C99.
因为结构体是 c 中的一等公民,所以赋值定义良好,这让您可以
找到您想要的。如果您确实在函数中声明了板,我建议将其声明为静态,因为您不修改它,因此持久更改是可以的,并且函数不必在每次调用时都增加堆栈。
Because structs are first class citizens in c, assignment is well defined, this lets you
Which seems to be what you want. If you do declare the board within the function, I would suggest declaring it static, because you don't modify it, so persistent changes are ok, and so that the function doesn't have to grow the stack as much on every call.