2009-12-17 134 views
0

我想創建一個符號表使用的結構數組的數組。如何創建一個數組的結構數組

現在我只是有結構的數組和它的創建是這樣的:

#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] 

我該怎麼做?我想要做什麼是有意義的?

回答

2

創建結構的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; 
    } 
} 
3
struct tableEntry symbolTable[MAXSCOPE]; 

,並使用例如

symbolTable[scope][entry].value; 
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; 
相關問題