LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C Enumerated Types / Function Pointer Errors (https://www.linuxquestions.org/questions/programming-9/c-enumerated-types-function-pointer-errors-499192/)

Centinul 11-06-2006 03:18 PM

C Enumerated Types / Function Pointer Errors
 
I have the following C code:

Code:

enum func_err_code {FUNC1, FUNC2, FUNC3};

// Function definition
int handle_error( int (*error_func_ptr)(func_err_code,int,void *),func_error_code function_code, int error_code_int, void *error_code_point);

// Another function definition
int pcre_error(func_err_code function_code, int error_code_int, void *error_code_point);

This is a header file that I created. When I try and compile the header file with the .c file of a similar name I get the following errors:

Pertaining to the first function:
Code:

error_handler.h:17: error: expected ')' before 'int'
error_handler.h:17: error expected ';', ',' or ')' before 'func_err_code'

Pertaining to the second function:
Code:

error_handler.h:22 error: expected ')' before 'function_code'
I have no idea how to fix either of these two errors. With regards to the enumerated types I've tried placing "enum" in the functino declaration but that throws the following warning:
Code:

error_handler.h:24 warning: 'enum func_err_code' declared inside parameter list
error_handler.h:24 warning: it's scope is only this definition or declaration, which is probably not what you want.

Any insight into solving this problem so I can get my error handler working is greatly appreciated. Thanks.

tuxdev 11-06-2006 03:33 PM

Try using a typedef for your function pointers. It is good practice anyway.

Quigi 11-06-2006 03:41 PM

Quote:

Originally Posted by Centinul
Code:

enum func_error_code {FUNC1, FUNC2, FUNC3};

// ...
// Another function definition
int pcre_error(func_err_code function_code, int error_code_int, void *error_code_point);


Typo?

(Linuxquestions wants more characters. Linuxquestions wants more characters.)

Centinul 11-06-2006 05:21 PM

Quote:

Originally Posted by tuxdev
Try using a typedef for your function pointers. It is good practice anyway.

Can I get an example please? I'm very new to function pointers. Thanks.


Quigi -- Checked the typo and that was a forum post error on my part. I'm still getting the same errors as stated above. Thanks.

Quigi 11-07-2006 11:26 AM

Hi Centinul,

Your problem was omitting "enum" in C. (Your original code is correct C++, I compiled it without a complaint. I'd switch the language :) ) The following modified error_handler.h compiles fine as C:
Code:

enum func_err_code {FUNC1, FUNC2, FUNC3};

// Function definition
int handle_error( int (*error_func_ptr)(enum func_err_code,int,void *),
                  enum func_err_code function_code,
                  int error_code_int, void *error_code_point);

// Another function definition
int pcre_error(enum func_err_code function_code,
              int error_code_int, void *error_code_point);

Tuxdev is right, typedefs make working with function pointers cleaner. You could also use a typedef instead of adding all those "enum" keywords.

BTW, you can just compile error_handler.h (and not the .c file of similar name) to trim down the problem for us. And the line numbers where you report errors seem wrong for a file of 7 lines.

Centinul 11-07-2006 03:08 PM

Quote:

Originally Posted by Quigi
Hi Centinul,

Your problem was omitting "enum" in C. (Your original code is correct C++, I compiled it without a complaint. I'd switch the language :) ) The following modified error_handler.h compiles fine as C:

I initially tried putting enum in there. I also stated that in my original post, and listed the associated errors when I placed that there.

Quote:

Originally Posted by Quigi
Tuxdev is right, typedefs make working with function pointers cleaner. You could also use a typedef instead of adding all those "enum" keywords.

I don't know how to use typedefs with enumerated types or functions pointers. Example please?

Quote:

Originally Posted by Quigi
BTW, you can just compile error_handler.h (and not the .c file of similar name) to trim down the problem for us. And the line numbers where you report errors seem wrong for a file of 7 lines.

I know that. I just put up the .h file because it contained the function declarations. It was also the file that was generating errors when I tried to compile. Yes I know the line numbers are wrong it was because I didn't include the complete file. I only took out the relevant snippets that were generating errors.

::EDIT:: Interestingly enough I tried putting the enum keywords back and it worked. I'm not sure why it failed the first time. Thanks for all the help.

jim mcnamara 11-07-2006 03:27 PM

Example:
Code:

typedef int (*F)(int, char *, char * );

int func1(int, char *, char *);
int func4(int, char *, char *);
int func3(int, char *, char *);
int func2(int, char *, char *);

int foo(int which_one, char *a, char *b)
{
  F fx_array[4]={func1,func2,func3,func4};
  int i=0;
  int retval= fx_array[which_one](i,a,b);
  return retval;
}

This code uses a variable, which_one, to call one function from a list.
It's just like a switch where you call differnet functions depending
on the argument to switch().

jim mcnamara 11-07-2006 03:30 PM

More than you ever wanted to know about function pointers

http://www.newty.de/fpt/index.html

Quigi 11-07-2006 07:46 PM

Quote:

Originally Posted by Centinul
I don't know how to use typedefs with enumerated types or functions pointers. Example please?

Let me rewrite your example using one of each.
Code:

// type f_err_code is short for "enum func_err_code"
typedef enum func_err_code {FUNC1, FUNC2, FUNC3} f_err_code;

typedef int error_func (f_err_code, int, void *);

// Function definition
int handle_error( error_func *error_func_ptr, f_err_code function_code,
                  int error_code_int, void *error_code_point);

// Another function definition
int pcre_error(f_err_code function_code,
              int error_code_int, void *error_code_point);

BTW, that name "func_err_code" is now optional. It's not used anywhere, and you can as well drop it.


All times are GMT -5. The time now is 09:13 PM.