I'm currently working on a DBMS as a college project, I'm having issues at printing out an array of strings which is defined in an header file.
I've tried playing around with pointers but I guess the problem is within the definition of the array, which being in another file can't be retrieved properly.
Here's metacommand.h file
typedef enum {NORMAL, DB, TABLE} Mode;
const char* normalModeMetaCommands[] = {".help", ".exit", ".db-mode", ".table-mode"};
const char* dbModeMetaCommands[] = {".help", ".exit", ".workingdir", ".dbs", ".db"};
const char* tableModeMetaCommands[] = {".help", ".exit", ".tables", ".table", ".index", ".select"};
const char* getModeName(Mode mode);
const char* getModeInputPrompt(Mode mode);
const char* getModeMetaCommands(Mode mode);
static void print_metacommands();
And here's some snippets from database.c file
static Mode mode = NORMAL;
const char* getModeMetaCommands(Mode mode)
{
switch (mode)
{
case NORMAL: return *normalModeMetaCommands;
case DB: return *dbModeMetaCommands;
case TABLE: return *tableModeMetaCommands;
}
}
static void print_metacommands(){
size_t size = sizeof(getModeMetaCommands(mode))/sizeof(getModeMetaCommands(mode)[0]);
for(int i=0; i<size; i++){
printf("%s\n", getModeMetaCommands(mode)[i]);
}
}
I have defined some functions that change the enum value in the database.c file, so that in each mode (NORMAL, DB and TABLE) I can print out an exclusive list of metacommands.
EDIT
My function print_metacommands()
doesn't print those char pointers array (*normalModeMetaCommands[], *dbModeMetaCommands[], *tableModeMetaCommands[]).
Output is a segFault.
If I change:
const char* getModeMetaCommands(Mode mode);
to:
const char** getModeMetaCommands(Mode mode);
And:
const char* getModeMetaCommands(Mode mode)
{
switch (mode)
{
case NORMAL: return *normalModeMetaCommands;
case DB: return *dbModeMetaCommands;
case TABLE: return *tableModeMetaCommands;
}
}
to:
const char** getModeMetaCommands(Mode mode)
{
switch (mode)
{
case NORMAL: return normalModeMetaCommands;
case DB: return dbModeMetaCommands;
case TABLE: return tableModeMetaCommands;
}
}
The output is only ".help" because it seems like:
size_t size = sizeof(getModeMetaCommands(mode))/sizeof(getModeMetaCommands(mode)[0]);
//evaluated to: size=1
So, how can I print out those char pointers array via static void print_metacommands();
?