LinuxQuestions.org
Visit Jeremy's Blog.
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-25-2007, 05:25 PM   #1
spursrule
LQ Newbie
 
Registered: Feb 2006
Distribution: LFS
Posts: 26

Rep: Reputation: 15
( C ) How do you declare a function pointer where a parameter is a function pointer?


I'm writing a simple program to time various sorts, which can be configured by command-line switches.

Assume I have a family of sorts with prototypes of the same format as:

Code:
int insertion_sort(void *A, int n, int sz_elem,
      int (*cmp)(const void *p, const void *q))
i.e., like Strousstrup does on pg 158 of "The C++ Programming Language" in his shellsort.


Where cmp is a comparison predicate and sz_elem is the size of any one element (all the same size) in the array A.

What I want to do is have a function pointer called sort, so I can assign the correct function to sort, and just call

Code:
(*sort)(A,n,sizeof(int),some_cmp)
if say, A is an array of ints.

My first thought was to declare sort as:
Code:
int (*sort)(void*, int, int,
          int (*)(const void*, const void *));
However, gcc told me no. I know it can be done pretty easily as
Code:
typedef  int (*Cmp_fcn)(const void*, const void*);
int (*sort) (void*, int, int, Cmp_fcn);
However, how could I do it without the typedef?
Code:
int (*sort) (void*, int, int, int);
gives a warning about assignment from incompatible pointer type, so it can't be right (right?).

Please don't laugh at me for looking in a C++ book for a way to write in C-style instead of C++-style.
lol

Thanks
 
Old 11-25-2007, 05:43 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
At what point did gcc say "no"? Was it the declaration of sort, or its assignment? I don't see a reason to not use a typedef, or a reason why your non-typedef declaration shouldn't work. It's a hell of a lot less confusing to typedef for someone who needs to read your code and it gives you more time to figure out real problems
ta0kira

PS Comeau likes both versions, so it could be a gcc bug. Or it could be that your problem is with a cast?

Last edited by ta0kira; 11-25-2007 at 05:50 PM.
 
Old 11-25-2007, 06:04 PM   #3
spursrule
LQ Newbie
 
Registered: Feb 2006
Distribution: LFS
Posts: 26

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ta0kira View Post
At what point did gcc say "no"? Was it the declaration of sort, or its assignment? I don't see a reason to not use a typedef, or a reason why your non-typedef declaration shouldn't work. It's a hell of a lot less confusing to typedef for someone who needs to read your code and it gives you more time to figure out real problems
ta0kira

PS Comeau likes both versions, so it could be a gcc bug. Or it could be that your problem is with a cast?

Oh man, chalk this up to programmer error. I must have frustratingly typed something incorrectly the first time, because now it works fine with the exact un-typedef-ed form of the declaration from above. I was asking because I felt like I might have been missing some big-picture thing with function pointers if I couldn't prototype/declare a function that had a function pointer as a parameter. I must have forgot a * somewhere or something. I completely agree on using the typedef-ed version to improve (or maybe give?) readability.

Thanks though.
 
Old 11-26-2007, 04:21 PM   #4
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 36
my gcc 4.1 likes this:
Code:
#include <stdlib.h>
typedef
void (*ss_t) (void *,
               size_t,
               size_t,
               int (*cmp)(const void *, const void *));
void shell_sort(void *, size_t, size_t, int (*cmp)(const void *, const void *));
void ins_sort(void *, size_t, size_t, int (*cmp)(const void *, const void *));

ss_t sorts[3]={shell_sort,
               ins_sort,
               qsort};
 
Old 11-27-2007, 04:37 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
I would ddeclare a function pointer type as a typedef first
like the X libs do, as in Intrinsic.h

Code:
typedef void (*XtActionProc)(

    Widget              /* widget */,
    XEvent*             /* event */,
    String*             /* params */,
    Cardinal*           /* num_params */

);


... later on ....
extern void XtRegisterGrabAction(

    XtActionProc        /* action_proc */,
    _XtBoolean          /* owner_events */,
    unsigned int        /* event_mask */,
    int                 /* pointer_mode */,
    int                 /* keyboard_mode */

);
It makes it all a bit more readable, n'est-ce pas?
 
Old 11-27-2007, 07:56 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by spursrule View Post
I completely agree on using the typedef-ed version to improve (or maybe give?) readability.
In one of my projects, I have a header with about 30 typedefs mostly meaning the same things like void*, uint32_t, etc. It tells people what you intend an argument or variable to mean and it also makes maintenance a lot easier. For example, I realized that I needed to change a certain type of argument from unsigned to signed in a 150+ file project and all I had to do was change one line in the typedef file.
ta0kira
 
  


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 09:03 PM.

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