Hi.

I am trying to create a look up table in C. I have attempted it, but there are compilation errors occurring.
Can anyone tell me what i'm doing wrong/offer any advice?
I have in my header file the following:

struct 	
{	
    (void)(*function)();	
    const char* functionName;
} 
lookUpTable[] = { {&TSL1_ReadMemory32, "ReadMemory32"}, {NULL, NULL} }; // Two nulls to indicate end of array...

Errors are as follows:
c:\projects\umaster3xxxc++\inteldriver\tsl1\tsl1_commands.h(288) : error C2062: type 'void' unexpected
c:\projects\umaster3xxxc++\inteldriver\tsl1\tsl1_commands.h(288) : error C2238: unexpected token(s) preceding ';'
c:\projects\umaster3xxxc++\inteldriver\tsl1\tsl1_commands.h(290) : error C2059: syntax error : '{'
c:\projects\umaster3xxxc++\inteldriver\tsl1\tsl1_commands.h(290) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
TSL1.cpp
c:\projects\umaster3xxxc++\inteldriver\tsl1\tsl1_commands.h(288) : error C2062: type 'void' unexpected
c:\projects\umaster3xxxc++\inteldriver\tsl1\tsl1_commands.h(288) : error C2238: unexpected token(s) preceding ';'
c:\projects\umaster3xxxc++\inteldriver\tsl1\tsl1_commands.h(290) : error C2059: syntax error : '{'
c:\projects\umaster3xxxc++\inteldriver\tsl1\tsl1_commands.h(290) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
Error executing cl.exe.

The parentheses around void seem to be the problem. Does this produce the same error?

struct 	
{	
    void (*function)();	
    const char* functionName;
} 
lookUpTable[] = { {&TSL1_ReadMemory32, "ReadMemory32"}, {NULL, NULL} };

Try something like

#include <stdio.h>

typedef void (*myfunction)(void);

void TSL1_ReadMemory32()
{
	fputs("called!\n", stdout);
}


struct 
{
	myfunction thefunct;
	const char* functionName;
}lookUpTable[] = { {TSL1_ReadMemory32, "ReadMemory32"}, {NULL, NULL} };


int main() 
{
	lookUpTable[0].thefunct();
	
	return 0;
}

Try something like

#include <stdio.h>

typedef void (*myfunction)(void);

void TSL1_ReadMemory32()
{
	fputs("called!\n", stdout);
}


struct 
{
	myfunction thefunct;
	const char* functionName;
}lookUpTable[] = { {TSL1_ReadMemory32, "ReadMemory32"}, {NULL, NULL} };


int main() 
{
	lookUpTable[0].thefunct();
	
	return 0;
}

gerard4143:

Why are you suggesting a typedef for the function? I am not suggesting that that you are wrong; I am just interested in what you see as the advantages and disadvantages are of the typedef over direct declaration:

#include <stdio.h>

void TSL1_ReadMemory32(void)
{
	fputs("called!\n", stdout);
}


struct 
{
	void (*function)(void);
	const char* functionName;
}lookUpTable[] = { {TSL1_ReadMemory32, "ReadMemory32"}, {NULL, NULL} };


int main() 
{
	lookUpTable[0].thefunct();
	
	return 0;
}

Do you generally define a typedef for all your types? (If so, that answers the question.)

gerard4143:

Why are you suggesting a typedef for the function? I am not suggesting that that you are wrong; I am just interested in what you see as the advantages and disadvantages are of the typedef over direct declaration:

#include <stdio.h>

void TSL1_ReadMemory32(void)
{
	fputs("called!\n", stdout);
}


struct 
{
	void (*function)(void);
	const char* functionName;
}lookUpTable[] = { {TSL1_ReadMemory32, "ReadMemory32"}, {NULL, NULL} };


int main() 
{
	lookUpTable[0].thefunct();
	
	return 0;
}

Do you generally define a typedef for all your types? (If so, that answers the question.)

I find the code cleaner and easier to read with a typedef...Personal preference.

Guys,
I am still having the syntax errors when building:

typedef void (*myfunction)(void);
struct
{
    myfunction thefunct;
    const char* functionName;
}lookUpTable[] = { {TSL1_ReadMemory32, "ReadMemory32"}, {NULL, NULL} };

Errors are as follows:
c:\projects\umaster3xxxc++\inteldriver\tsl1\tsl1_commands.h(300) : error C2059: syntax error : '{'
c:\projects\umaster3xxxc++\inteldriver\tsl1\tsl1_commands.h(300) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
TSL1_Commands.cpp
c:\projects\umaster3xxxc++\inteldriver\tsl1\tsl1_commands.h(300) : error C2059: syntax error : '{'
c:\projects\umaster3xxxc++\inteldriver\tsl1\tsl1_commands.h(300) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body

@Luther von Wulf,

struct
      {
      void (*function)();
      const char* functionName;
      }
      lookUpTable[] = { {&TSL1_ReadMemory32, "ReadMemory32"}, {NULL, NULL} };

Produces the following errors:
TSL1.cpp
c:\projects\umaster3xxxc++\inteldriver\tsl1\tsl1_commands.h(300) : error C2059: syntax error : '{'
c:\projects\umaster3xxxc++\inteldriver\tsl1\tsl1_commands.h(300) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
TSL1_Commands.cpp
c:\projects\umaster3xxxc++\inteldriver\tsl1\tsl1_commands.h(300) : error C2059: syntax error : '{'
c:\projects\umaster3xxxc++\inteldriver\tsl1\tsl1_commands.h(300) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
Error executing cl.exe.

Does it matter if this struct is defined in the header file or the source file? I'd like to keep it in the header file, as I have ~200 entries to fill.

Try compiling the small program I posted. Did it compile and execute correctly?

@gerard4143,
Yes, yours compiles fine. What am I doing wrong?
I have a header file and class. In the header file, TSL1_Common.h I have:

int TSL1_ReadMemory32 (char *sCmdInt[], char returnData[256]);

In my source file, TSL1_Common.cpp I have:

int CTSL1_Commands::TSL1_ReadMemory32 (char *sCmdInt[], char returnData[256])
/*******************************************************************************

	Name:			TSL1_ReadMemory32
					
	Description:	This function handles the call to read memory 32
					
	Inputs:			sCmdInt - A pointer to the command array 
					
	Outputs:		returnData - The result of the operation
*******************************************************************************/
{
    int iError = DIAG_ReadMem32(sCmdInt[1], returnData);
    return iError;
}

I have tried filling in the typedef/struct at this point in the source file, but i'm still getting the following errors:
C:\Projects\uMaster3xxxC++\IntelDriver\TSL1\TSL1_Commands.cpp(223) : error C2065: 'TSL1_ReadMemory32' : undeclared identifier
C:\Projects\uMaster3xxxC++\IntelDriver\TSL1\TSL1_Commands.cpp(223) : error C2440: 'initializing' : cannot convert from 'int' to 'void (__cdecl *)(void)'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Projects\uMaster3xxxC++\IntelDriver\TSL1\TSL1_Commands.cpp(223) : error C2440: 'initializing' : cannot convert from 'char [13]' to 'void (__cdecl *)(void)'
There is no context in which this conversion is possible
Error executing cl.exe.

Well right away your function definition is different than your structure function definition...

int TSL1_ReadMemory32 (char *sCmdInt[], char returnData[256]);

compared to

void (*function)(void);

Well right away your function definition is different than your structure function definition...

int TSL1_ReadMemory32 (char *sCmdInt[], char returnData[256]);

compared to

void (*function)(void);

Yes, I have tried to amend that as follows:

typedef int (*myfunction)(char *[], char[]);

But still compilation errors:

C:\Projects\uMaster3xxxC++\IntelDriver\TSL1\TSL1_Commands.cpp(223) : error C2065: 'TSL1_ReadMemory32' : undeclared identifier
C:\Projects\uMaster3xxxC++\IntelDriver\TSL1\TSL1_Commands.cpp(223) : error C2440: 'initializing' : cannot convert from 'int' to 'int (__cdecl *)(char *[],char [])'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Projects\uMaster3xxxC++\IntelDriver\TSL1\TSL1_Commands.cpp(223) : error C2440: 'initializing' : cannot convert from 'char [13]' to 'int (__cdecl *)(char *[],char [])'

You mention that you have a class in your code. Does this code have a mixture of C and C++?

It is actually C++ code, but we are restricted to using C library functions. The only real C++ element is the OOP. For this reason, I have posted in the C forum. Also, as I have the OOP functionality, this was why I was trying to keep the struct (if possible) in a header file

Well I tried this and it works.

#include <stdio.h>

typedef int (*myfunction)(char *[], char[]);

int TSL1_ReadMemory32 (char *sCmdInt[], char returnData[256])
{
	fputs(sCmdInt[0], stdout);
	fputs(returnData, stdout);
	return 12;
}

struct 	
{	
    myfunction thefunc;	
    const char* functionName;
} 
lookUpTable[] = { {&TSL1_ReadMemory32, "ReadMemory32"}, {NULL, NULL} };

int main(int argc, char**argv)
{
	return lookUpTable[0].thefunc(argv, "test data\n");
}

Maybe if you should include this functionality in a separate C source file and then compile as C code. Once compiled try linking it into the main C++ program....Note I never tried mixed C/C++ programming on a Windows environment.

Yes, that works fine if I create my function without the class specifier. However, using

int CTSL1_Commands::TSL1_ReadMemory32 (char *sCmdInt[], char returnData[256])

I'm still getting the same errors. Any ideas?
I need the class specifier as i'm calling this function from another class

pTSL1_Common->TSL1_ReadMemory32(...)

Yes, that works fine if I create my function without the class specifier. However, using

int CTSL1_Commands::TSL1_ReadMemory32 (char *sCmdInt[], char returnData[256])

I'm still getting the same errors. Any ideas?
I need the class specifier as i'm calling this function from another class

pTSL1_Common->TSL1_ReadMemory32(...)

Where did the class specifier come from? This is the first time you mentioned it! If your trying to implement a class function, then this should be posted in the C++ section.

@gerard4143,
Yes, yours compiles fine. What am I doing wrong?
I have a header file and class. In the header file, TSL1_Common.h I have:

int TSL1_ReadMemory32 (char *sCmdInt[], char returnData[256]);

In my source file, TSL1_Common.cpp I have:

int CTSL1_Commands::TSL1_ReadMemory32 (char *sCmdInt[], char returnData[256])
/*******************************************************************************

	Name:			TSL1_ReadMemory32
					
	Description:	This function handles the call to read memory 32
					
	Inputs:			sCmdInt - A pointer to the command array 
					
	Outputs:		returnData - The result of the operation
*******************************************************************************/
{
    int iError = DIAG_ReadMem32(sCmdInt[1], returnData);
    return iError;
}

I have tried filling in the typedef/struct at this point in the source file, but i'm still getting the following errors:
C:\Projects\uMaster3xxxC++\IntelDriver\TSL1\TSL1_Commands.cpp(223) : error C2065: 'TSL1_ReadMemory32' : undeclared identifier
C:\Projects\uMaster3xxxC++\IntelDriver\TSL1\TSL1_Commands.cpp(223) : error C2440: 'initializing' : cannot convert from 'int' to 'void (__cdecl *)(void)'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
C:\Projects\uMaster3xxxC++\IntelDriver\TSL1\TSL1_Commands.cpp(223) : error C2440: 'initializing' : cannot convert from 'char [13]' to 'void (__cdecl *)(void)'
There is no context in which this conversion is possible
Error executing cl.exe.

I did mention that classes were involved. Apologies, the only reason I posted in the C forum is that I have posted in C++ forums before and have been that I should post in the C forums (plus, even though I mention that I can only use C library functions, people still tend to post their C++ solutions).

I did mention that classes were involved. Apologies, the only reason I posted in the C forum is that I have posted in C++ forums before and have been that I should post in the C forums (plus, even though I mention that I can only use C library functions, people still tend to post their C++ solutions).

If your talking about member functions then its a C++ solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.