在结构初始化中初始化结构?

发布于 2024-12-03 10:58:54 字数 514 浏览 0 评论 0原文

这可能听起来有点愚蠢,但我必须知道,因为我正在用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里寻她 2024-12-10 10:58:54

它可以简单地完成

void initialize_columns()
{
    bingo_board board = {
      {1, 2, 3, 4, 5},
      {6, 7, 8, 9, 10},
      {11, 12, 13, 14, 15},
      {16, 17, 18, 19, 20},
      {21, 22, 23, 24, 25}
    };
}

或 甚至

void initialize_columns()
{
    bingo_board board = {
      1, 2, 3, 4, 5,
      6, 7, 8, 9, 10,
      11, 12, 13, 14, 15,
      16, 17, 18, 19, 20,
      21, 22, 23, 24, 25
    };
}

不需要“标记”每一行。不过,标记语法在 C99 中可用,并且示例中的内容对于 C99 来说已经是正确的。

It can be done simply as

void initialize_columns()
{
    bingo_board board = {
      {1, 2, 3, 4, 5},
      {6, 7, 8, 9, 10},
      {11, 12, 13, 14, 15},
      {16, 17, 18, 19, 20},
      {21, 22, 23, 24, 25}
    };
}

Or even as

void initialize_columns()
{
    bingo_board board = {
      1, 2, 3, 4, 5,
      6, 7, 8, 9, 10,
      11, 12, 13, 14, 15,
      16, 17, 18, 19, 20,
      21, 22, 23, 24, 25
    };
}

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.

下雨或天晴 2024-12-10 10:58:54

因为结构体是 c 中的一等公民,所以赋值定义良好,这让您可以

static bingo_board initial_board = {
    {1, 2, 3, 4, 5},
    {6, 7, 8, 9, 10},
    {11, 12, 13, 14, 15},
    {16, 17, 18, 19, 20},
    {21, 22, 23, 24, 25}
};


void init_board(bingo_board *b)
{
    *b = initial_board;
}

找到您想要的。如果您确实在函数中声明了板,我建议将其声明为静态,因为您不修改它,因此持久更改是可以的,并且函数不必在每次调用时都增加堆栈。

Because structs are first class citizens in c, assignment is well defined, this lets you

static bingo_board initial_board = {
    {1, 2, 3, 4, 5},
    {6, 7, 8, 9, 10},
    {11, 12, 13, 14, 15},
    {16, 17, 18, 19, 20},
    {21, 22, 23, 24, 25}
};


void init_board(bingo_board *b)
{
    *b = initial_board;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文