How to create an array of an array of structs
I am trying to create a symbol table using an array of an array of structs.
Right now I just have an array of structs and it's created like this:
#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];
It works, but I want to mak开发者_如何学运维e something like this:
symbolTable[MAXSCOPE].tableEntry[MAXSIZE]
How would I do that? Does it make sense what I'm trying to do?
struct tableEntry symbolTable[MAXSCOPE];
and use e.g.
symbolTable[scope][entry].value;
Create a 2 dimensional array of structs:
// 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];
You can now access individual entries like this:
// 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;
}
}
If you really wanted to access it that way...
#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];
This is how you can access the data
symbolTable[1].tableEntry[2].value = 1;
精彩评论