c++ - 在 C 和 C++ 中创建点数组

标签 c++ c arrays struct

有不同的方法可以达到相同的目的,但这基本上就是我想要做的:

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/

相关文章:

c++ - 创建具有多个类的库的推荐方法是什么?

c++ - 使用 `bdrv_pread(..)` 或替代品读取 qcow2 图像的内容

c++ - 非重叠连续子数组的最大长度和

c - 在 c 中按字典顺序打印 trie

javascript - 在数组中循环遍历数组

c++ - OpenCV SURF 类错误

c++ - FPU 控制函数是否与 x64_64 处理器相关?

c - 嵌套在循环中的公式将无法正确执行

java - 优化数组增量的性能

python - 将 numpy.ndarray 写入 xlsx 电子表格