我正在尝试使用结构数组的数组创建符号表。
现在我只有一个结构数组,它是这样创建的:
#define MAXSIZE 20 /* maximum number of symbols */
#define MAXSCOPE 10 /* maximum number of scope levels */
struct tableEntry {
char *name;
char *args;
int value;
int scope;
char *type;
int used;
} tableEntry [MAXSIZE];
它有效,但我想做这样的事情:
symbolTable[MAXSCOPE].tableEntry[MAXSIZE]
我该怎么做?我想做什么才有意义?
答案 0 :(得分:3)
struct tableEntry symbolTable[MAXSCOPE];
并使用例如
symbolTable[scope][entry].value;
答案 1 :(得分:2)
创建一个二维结构数组:
// Define the type
typedef struct tableEntry {
char *name;
char *args;
int value;
int scope;
char *type;
int used;
} tableEntry;
// Instantiate a 2D array of this type
tableEntry myArray[MAXSCOPE][MAXSIZE];
您现在可以访问以下各个条目:
// Initialise 'value' in each entry to 2
int scope=0;
int size=0;
for (; scope < MAXSCOPE; scope++)
{
for (; size < MAXSIZE; size++)
{
myArray[scope][size].value = 2;
}
}
答案 2 :(得分:2)
如果你真的想以这种方式访问它......
#define MAXSIZE 20 /* maximum number of symbols */
#define MAXSCOPE 10 /* maximum number of scope levels */
struct _table_entry_ {
char *name;
char *args;
int value;
int scope;
char *type;
int used;
};
struct _symbol_table_ {
_table_entry_ tableEntry[MAXSIZE];
}symbolTable[MAXSCOPE];
这是您访问数据的方法
symbolTable[1].tableEntry[2].value = 1;