【发布时间】:2019-04-04 06:10:08
【问题描述】:
在.h文件中
typedef enum
{A1,A2}struct_A;
typedef struct
{const struct_A G;} struct_B;
typedef struct
{const struct_B * F;} struct_C;
typedef struct
{const struct_C *E;} struct_D;
在 .c 文件中
const struct_D temp;
如何设置/更改:
temp->E[i].F[0].G
【问题讨论】:
-
请确保您发现自动大写并修复它 - 手机是在 SO 上输入代码的威胁。
Typedef和Const的使用最终是粗心的,程序员不能粗心。 -
temp在定义的时候需要初始化(这是一个比较复杂的操作);此后,您不应该尝试修改存储(通过名称temp)。如果要修改存储,不要使用const。如果您尝试修改存储,编译器会抱怨(我假设您不会询问它是否没有抱怨),并且您调用 undefined behavior 这总是 A Bad Thing!™ -
struct_B b = {A1}; struct_C c = {&b}; const struct_D temp = {&c}; -
@Tiến Nguyễn 不确定这是否是题外话,但你为什么需要这样的嵌套结构。由于 struct_B 和 struct_C 具有指针,因此 B.G 中的任何更改都会自动更改
temp.E->F->G中的值。除非这是一个好奇心/学习阶段/家庭作业问题,否则我认为没有理由在这里使用const关键字。
标签: c pointers struct constants