Typedef 结构到结构数组
typedef structure to array of structures
我确定这是一个脑子放屁,但我错过了一些东西,谷歌搜索似乎没有带来任何东西。
struct item
{
int a;
int b;
int c;
};
typedef item *itemcatalog;
所以"itemcatalog"只是"item"的数组。
const item items[] = {{1,2,3},{4,5,6}};
const item *collection = items; // OK
const itemcatalog catalog = items; // "item const *" is incompatible with "itemcatalog"
这个想法是"itemcatalog"更具描述性,并显示需要一组项目,而不仅仅是指向单个项目的指针。
编辑:修复错别字。
首先,有一个错别字:
在失败的行上,您忘记命名对象。
const itemcatalog collection2 = items;
现在,让我们也尝试将 const 限定符应用于这些变量:
当我们这样做时,我们仍然会收到一个错误:
foo.cc:14:19: error: cannot initialize a variable of type 'const itemcatalog' (aka 'item *const') with an lvalue of type 'const item [2]'
const itemcatalog catalog = items;
^ ~~~~~
1 error generated.
为了解决这个问题,我们需要意识到在这种情况下我们实际上需要两个 typedef:
struct item
{
int a;
int b;
int c;
};
typedef item *itemcatalog;
typedef const item *constitemcatalog;
const item items[] = {{1,2,3},{4,5,6}};
const item *collection = items;
const constitemcatalog collection2 = items;
item mutable_items[] = {{1,2,3},{4,5,6}};
const item *collection3 = mutable_items;
const itemcatalog collection4 = mutable_items;
在各种集合对象上,我们应用const
告诉我们是否可以移动指针。 constitemcatalog
vs itemcatalog
告诉我们是否可以修改指针指向的数据。
相关文章:
- C++将文本文件中的数据读取到结构数组中
- JSON转换为nlohmann JSON-lib中的结构数组
- 需要帮助将结构数组传递给函数
- 在 64 位边界上对齐C++结构数组?
- 带有枚举方向/类型的气泡排序结构数组
- 在C++中访问结构数组中的数据
- 从指向结构数组的指针中提取元素
- 如何制作一个地图,其中的值是C++中的结构数组
- 如何在函数中显示结构数组
- 包含作为成员C++函数的结构数组
- 如何修复函数中的 fstream 文件输入以将正确的信息存储在结构数组中?
- std::transform 将一个结构数组复制到另一个结构数组
- 将非托管结构数组封送到托管数组
- 显示结构数组
- 结构数组中所有类似字段的总和
- 结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配
- 在 glsl opengl 中将嵌套结构数组作为统一传递
- 使用初始化列表填充C++中的多维结构数组时出现问题
- 如何将字符串字符与结构数组进行比较?
- C++结构数组