有不同的方法可以达到相同的目的,但这基本上就是我想要做的:
typedef struct point_t {
unsigned int x;
unsigned int y
} point_t;
point_t 点[64] = { {23, 67}, {123, 9}, {55, 0} ... }
我只想创建一个 xy 坐标常量数组并像这样读取它们:
i = points[0].x
j = points[0].y
这在 C 和 C++ 中有效吗?
最佳答案
声明结构时出现语法错误,在 unsigned int y
之后缺少分号,无论如何,检查其是否有效的最佳方法是使用 c/例如,c++编译器,尝试编译并运行这个:
#include <math.h>
#include <stdio.h>
typedef struct point_t {
unsigned int x;
unsigned int y;
} point_t;
int main(int argc, char *argv[]) {
point_t points[64] = {{23, 67}, {123, 9}, {55, 0}};
for (int i = 0; i < 64; i++) {
printf("%d %d\n", points[i].x, points[i].y);
}
}
您应该看到如何填充前 3 个点,并且结构数组的其余部分将被 0 填充。
关于c++ - 在 C 和 C++ 中创建点数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43233123/