如何在结构中索引指针类型的结构数组



在头文件中,我声明了一些结构,如下面的

typedef struct ScopeStack {
ste *entry[SCOPE_STACK_SIZE];
int top;
} ScopeStack;
typedef struct ste {
char *name;
decl *declaration;
struct ste *prev;
} ste;
typedef struct decl{
int declclass; /* DECL class: VAR, CONST, FUNC, TYPE */
decl *type; /* VAR, CONST: pointer to its TYPE decl*/
int value; /* CONST: value of integer constant */
float real_value; /* CONST: value of float constant */
ste *formals; /* FUNC: pointer to formal argument list */
decl *returntype; /* FUNC: pointer to return TYPE decl*/
int typeclass; /* TYPE: type class: INT, array, ptr, … */
decl *elementvar;/* TYPE (array): ptrto element VAR decl*/
int num_index; /* TYPE (array): number of elements */
ste *fieldlist; /* TYPE (struct): ptrto field list */
decl *ptrto; /* TYPE (pointer): type of the pointer */
int size; /* ALL: size in bytes */
ste **scope; /* VAR: scope when VAR declared */
decl *next; /* For list_of_variablesdeclarations */
} decl;

如果我声明像这样的变量

ScopeStack *scopestack;

我很好奇我是否可以通过索引来访问scopestack内的ste*类型数组

scopestack->entry[scopestack->top];

假设scopestackentry在代码中初始化,那么您可以毫无问题地访问数组条目(如果您想要的话,就像这个scopestack->entry[scopestack->top];(。不要让语句中的scopestack->top部分混淆您。

你想做的基本上是:

index = scopestack->top;
spopestack->entry[index];

其与CCD_ 5相同。

最新更新