在 C 和 C++ 中创建点数组
Creating array of points in C and C++
有不同的方法可以达到相同的目的,但这基本上是我想做的:
typedef struct point_t {
unsigned int x;
unsigned int y
} point_t;
point_t points[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 %dn", points[i].x, points[i].y);
}
}
您应该看到您如何填充前 3 个点,其余的结构数组将被 0 填充。
相关文章:
- 在将 new 与指针一起使用时,创建数组的指定长度
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- 合并排序不排序自创建数组类 c++
- 基于参数创建数组
- 使用new创建数组而不声明大小
- c++用输入数据创建数组
- 从类 c++ 动态创建数组
- 如何使用 void 函数从类创建数组
- 未知大小的数组作为类成员,用于在运行时(对象创建时间)创建数组的对象
- 如何在不知道Arduino中知道其大小的情况下创建数组和存储值
- 使用动态分配创建数组并将元素插入其中
- 是否可以使用C 函数中的const int参数创建数组
- 如何在C++函数中创建数组
- 使用 ::std::vector 创建数组
- 在编译时以增量方式创建数组
- Numpy C API - 使用 PyArray_Descr 创建数组会导致段错误
- 为什么当我尝试在类中创建数组时"invalid use of non-static data member"我会收到错误?
- 取消引用指针以创建数组的副本
- C++ 在堆上创建数组而不对其进行初始化
- 数组的维度是在创建数组的那一刻确定的,以后不能更改的,这是真的吗?