-1

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();?

1 Answers1

0

There's many basic errors here.

  • Never declare global variables anywhere, period.

  • Never define variables (or functions) in header files. This always leads to various linking hiccups and bugs where different instances of variables with the same name co-exist in different translation units.

    Instead, move all variable definitions and initializations to file scope inside a .c file, make them static and only let the caller access them through setter/getter functions. They will no longer be global, but note that they are still not thread safe.

  • You cannot use sizeof on a pointer, because pointers are not arrays. It gives the size of the pointer, not the length of the pointed-at string.

Community
  • 1
  • 1
Lundin
  • 195,001
  • 40
  • 254
  • 396