Click to See Complete Forum and Search --> : Easy Question (I hope)


SHiVeR
June 1st, 2003, 02:54 PM
ok, this is what im trying to do. I have a program with 24 lookup tables defined in the globals:

const char lookup1[255][5] = {"blah","blah2","blah3".......};
const char lookup2[255][5] = {"blah","blah2","blah3".......};
const char lookup3[255][5] = {"blah","blah2","blah3".......};

Later in the program I need to reference these tables in a case statement, but these tables are user selected based upon what they want to see in the output. So the only way for the program to know is set another variable with the pointer to which table to use. I figured I could do this in an array such as:

char * prtarray[4];

And then define which table has been selected by assigning its pointer to the array, and then later pulling it out:

ptrarray[0] = &lookup1;

And then later I have the row I need out of the table, but the only way to reference it is through the pointer. So I was thinking of doing something like:

StrCat(strValue, (* ptrarray[0])[1]);

or something like that to pull the value out of the lookup table by referencing its pointer in an array previously set up. I would thus want strValue to contain "blah2". I know the syntax above isnt right, but that's what I want to do and is the best way for me to illustrate it. I've tried everything I can think of to get it to work, but I just can't do it.

ANY help would be greatly appreciated! Thanks in advance!

kuphryn
June 1st, 2003, 04:33 PM
There are several solutions. One solution is to define an array of "lookup" instead of a series of arrays.

const char lookup[3][3][5] = {{"blah","blah2","blah3"}, {"blah","blah2","blah3"}, {"blah","blah2","blah3"}};

Another solution is a vector of strings.

Kuphryn

SHiVeR
June 1st, 2003, 04:38 PM
the array of lookup is the best idea I've heard from anyone yet! Best and simplest answer!

Paul McKenzie
June 1st, 2003, 09:13 PM
You also have to realize that some of your strings in your example are not 5 characters. "Blah2" is 6 characters. Your example would not compile for this reason.

I would also typedef the array. Then it is easier to pass it to other functions if you need to pass it around. Also for C++ programs, global variables should be discouraged (you could say the same for C programs) -- instead create the table in main() and pass it around, or create a class that has a static member variable that contains the lookup table.

// In mylookup.h
#ifndef LOOKUP_H
#define LOOKUP_H
typedef char LookupTable[5][3][6];
class GlobalLookupTable
{
public:
LookupTable TheTable;
};
#endif

Main program:

#include <stdio.h>
#include <mylookup.h>

LookupTable GlobalLookupTable::TheTable = {
{"blah","blah2","blah3"},
{"blah","blah2","blah3"},
{"blah","blah2","blah3"},
{"blah","blah2","blah3"},
{"blah","blah2","blah3"}
};

void PrintLookupTable( );

int main()
{
PrintLookupTable( );
}

void PrintLookupTable( )
{
for ( int i = 0; i < 5; ++i)
for ( int j = 0; j < 3; ++j)
printf("%s\n",GlobalLookupTable::TheTable[i][j]);
}

Something like that. Then all you need to do is to include the file mylookup.h. This encapsulates the global data in a class, so you don't have to do the "extern" crud all over the place.

Regards,

Paul McKenzie