LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-06-2006, 03:18 PM   #1
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Rep: Reputation: 30
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.

Last edited by Centinul; 11-06-2006 at 05:20 PM.
 
Old 11-06-2006, 03:33 PM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
Try using a typedef for your function pointers. It is good practice anyway.
 
Old 11-06-2006, 03:41 PM   #3
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
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.)

Last edited by Quigi; 11-06-2006 at 03:43 PM.
 
Old 11-06-2006, 05:21 PM   #4
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Original Poster
Rep: Reputation: 30
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.
 
Old 11-07-2006, 11:26 AM   #5
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
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.

Last edited by Quigi; 11-07-2006 at 11:29 AM.
 
Old 11-07-2006, 03:08 PM   #6
Centinul
Member
 
Registered: Jun 2005
Distribution: Gentoo
Posts: 552

Original Poster
Rep: Reputation: 30
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.
 
Old 11-07-2006, 03:27 PM   #7
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
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().
 
Old 11-07-2006, 03:30 PM   #8
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
More than you ever wanted to know about function pointers

http://www.newty.de/fpt/index.html
 
Old 11-07-2006, 07:46 PM   #9
Quigi
Member
 
Registered: Mar 2003
Location: Cambridge, MA, USA
Distribution: Ubuntu (Dapper and Heron)
Posts: 377

Rep: Reputation: 31
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
calling a function from a pointer spx2 Programming 3 05-25-2006 04:52 PM
(I know I am inept) what is a pointer function in C ? cigarstub Programming 3 09-27-2005 05:06 PM
Function Pointer as an Argument in C trutnev Programming 5 05-24-2005 10:22 AM
void * pointer in function xailer Programming 23 01-16-2004 02:14 PM
c++ Pointer to Member Function Wondre Programming 0 02-15-2003 06:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration