【发布时间】:2011-09-09 04:52:55
【问题描述】:
这听起来可能有点愚蠢,但我必须知道,因为我正在用 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}
};
}
这可能吗?
【问题讨论】:
-
typedef struct { int cells[5][5]; } bingo_board;是一种更加循环友好的数据结构。 -
C 的新手。我认为它是一个数组,但你认为你可以更明确一点吗?谢谢。
-
它是一个二维整数数组(五个元素乘五个元素)。一个名为
x的bingo_board可以作为 x.cells[row][column] 访问。这比必须为每行或每列编写单独的案例更容易在循环中处理。在您的实现中,您必须为x.one.a和x.four.a编写单独的代码(您必须编写至少十个案例,每一行和每列一个)。