ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.)
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.
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.
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().
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.