在结构初始化中初始化结构? [英] initialize a struct within a struct initialization?

查看:58
本文介绍了在结构初始化中初始化结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这听起来可能有些愚蠢,但我必须知道,因为我正在用 C 编写宾果游戏板.

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}
    };
}

这可能吗?

推荐答案

很简单

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.

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.

这篇关于在结构初始化中初始化结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆