Pointers 如何在结构中索引指针类型的结构数组?
在头文件中,我声明了一些结构,如下所示Pointers 如何在结构中索引指针类型的结构数组?,pointers,indexing,struct,Pointers,Indexing,Struct,在头文件中,我声明了一些结构,如下所示 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 cl
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];
假设
scopestack
和entry
在代码中已初始化,那么您可以毫无问题地访问数组条目(如scopestack->entry[scopestack->top];
如果您需要的话)。
不要让语句的scopestack->top
部分迷惑您
你想做的基本上是:
index = scopestack->top;
spopestack->entry[index];
与
scopestack->entry[scopestack->top]相同代码>还有一步。当然可以,为什么不能?当然,前提是您已经初始化了所有指向某个对象的指针。