Jump to content

  • Log In with Google      Sign In   
  • Create Account

C - Typedef and function pointers


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • This topic is locked This topic is locked
25 replies to this topic

#1 I_Smell_Tuna   Members   -  Reputation: 96

Like
0Likes
Like

Posted 23 January 2006 - 01:53 PM

Is there any way to typedef a function pointer. Such as... typedef (*fun_ptr)() funptr; funptr ExAmPlE = (*some_function);

Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9669

Like
0Likes
Like

Posted 23 January 2006 - 01:56 PM

Yes, but it's still ugly.

typedef void (*funptr)(void);

void some_function(void);

funptr p_some_function = &some_function;

The name of the typedef goes where the name of the variable is when declaring a function pointer variable.

#3 LessBread   Moderators   -  Reputation: 1411

Like
0Likes
Like

Posted 23 January 2006 - 02:03 PM


funptr p_some_function = &some_function;


The address of operator isn't needed.


funptr p_some_function = some_function;


The function name supplies the address of the function.

#4 SiCrane   Moderators   -  Reputation: 9669

Like
0Likes
Like

Posted 23 January 2006 - 02:06 PM

It's a good coding habit; when you take the address of a pointer to member function, the ampersand is necessary.

#5 LessBread   Moderators   -  Reputation: 1411

Like
0Likes
Like

Posted 23 January 2006 - 02:17 PM

What does a member function in C look like? I didn't know that C had such things. C++ sure, but C?

#6 SiCrane   Moderators   -  Reputation: 9669

Like
0Likes
Like

Posted 23 January 2006 - 02:33 PM

And your point is that I should ignore good coding habits just because the file has a .c extension? Next you're going to tell me that I shouldn't bother to put

#ifdef __cplusplus
extern "C" {
#endif

in my C header files because C never defines __cplusplus, right?

#7 LessBread   Moderators   -  Reputation: 1411

Like
0Likes
Like

Posted 23 January 2006 - 02:37 PM

My point is that there are differences between C and C++ and what constitutes a good coding habit in C++ may not hold for C and vice versa.

#8 SiCrane   Moderators   -  Reputation: 9669

Like
0Likes
Like

Posted 23 January 2006 - 02:50 PM

Then choose a different example to pick on. It's still a good coding habit in C, where hungarian notation is much more common and a programmer with experience in either apps or system hungarian will be distracted by an assignment to a pointer type with a non-decorated symbol name not-preceeded by an ampersand. That is to say something like

lpfFunctionPointer = some_function;

will trigger a warning sign in the back of his head while

lpfFunctionPointer = &some_function;

won't.

#9 Alpha_ProgDes   Crossbones+   -  Reputation: 4692

Like
0Likes
Like

Posted 23 January 2006 - 03:00 PM

Quote:
Original post by SiCrane
And your point is that I should ignore good coding habits just because the file has a .c extension? Next you're going to tell me that I shouldn't bother to put

#ifdef __cplusplus
extern "C" {
#endif

in my C header files because C never defines __cplusplus, right?

Silly question, but...
I see the beginning { but where's the }?



#10 SiCrane   Moderators   -  Reputation: 9669

Like
0Likes
Like

Posted 23 January 2006 - 03:02 PM

Somewhere after your done declaring your functions and before the #endif of the inclusion guard you would put:

#ifdef __cplusplus
}
#endif


#11 I_Smell_Tuna   Members   -  Reputation: 96

Like
0Likes
Like

Posted 23 January 2006 - 03:06 PM

Cool, thanks guys.

#12 LessBread   Moderators   -  Reputation: 1411

Like
0Likes
Like

Posted 23 January 2006 - 03:11 PM

Quote:
Original post by SiCrane
Then choose a different example to pick on.


Chill out man, i'm not picking on you.



#13 MaulingMonkey   Members   -  Reputation: 1556

Like
0Likes
Like

Posted 24 January 2006 - 04:38 PM

Quote:
Original post by LessBread
Quote:
Original post by SiCrane
Then choose a different example to pick on.

Chill out man, i'm not picking on you.

No, but you're picking on the example/habbit, which is all he said you were doing :).

And you're both wrong. Anything you do in C (using or not using ampersands) cannot be considered a good programming habit, because you're doing it in C, which is a bad habit in and of itself :-p.

#14 I_Smell_Tuna   Members   -  Reputation: 96

Like
0Likes
Like

Posted 24 January 2006 - 05:06 PM

If there was a better all-purpose language out there people would be using it. ^^

#15 Sneftel   Senior Moderators   -  Reputation: 1781

Like
0Likes
Like

Posted 24 January 2006 - 05:15 PM

Quote:
Original post by I_Smell_Tuna
If there was a better all-purpose language out there people would be using it. ^^


Surfed the internet lately?

#16 MaulingMonkey   Members   -  Reputation: 1556

Like
0Likes
Like

Posted 24 January 2006 - 05:17 PM

Quote:
Original post by I_Smell_Tuna
If there was a better all-purpose language out there people would be using it. ^^


And they are.

If you take a look at the SourceForge.net Software Map....

C ranks 3rd (16485 projects)
C++ ranks 2nd (17440 projects)
Java ranks 1st (17662 projects)

In terms of number of projects developed using that language.

My comment (C = Bad Habit) was in jest since C has a few good uses (it's ABI for APIs, OS level work, and esoteric platform support come to mind) but when you start implying C is the best all-purpouse language, I have to disagree, and feel like supplying at least some anecdotal evidence to the contrary (i.e. which is the most used). I do think C (and C++) are overused, mainly out of habit/familiarity, which was what I was getting at with my original, overstated stab against C. I'm too lazy to form an opinion on Java, although I suspect I'd decide it was overused as well.

#17 Teknofreek   Members   -  Reputation: 331

Like
0Likes
Like

Posted 24 January 2006 - 07:16 PM

Since we're debating syntax choices, I actually prefer this syntax:

typedef void Function(void);

void some_function(void);

Function* func = &some_function;


Not a huge deal, but this feels a bit cleaner to me. The typedef isn't as hard to read and there's no need to use hungarian notation since it's obvious that it's a pointer.

Just my 2 cents.

-John


#18 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 12 February 2006 - 02:57 AM

for a function with no input you use a 32 bit pointer
such as
void somefunct(void);
void *functptr=somefunct;

but if you need to use input and returns then you must make room for the extra memory needed

int somefunct(int x, char y, float z);
int (*functptr)(int,char,float)=somefunct;

it makes space for 17 bytes = sizeof(int)+sizeof((void *))+sizeof(int)+sizeof(char)+sizeof(float)

typedef struct STUFFSTRUCT{
int (*somefunct)(int,char,float);
} STUFFSTRUCT;

STUFFSTRUCT stuff;
STUFFSTRUCT *stuffptr;

stuffptr->somefunct=somefunct;
int myint=stuffptr->somefunct(1,*"a",3.0f);



#19 Enigma   Members   -  Reputation: 1402

Like
0Likes
Like

Posted 12 February 2006 - 03:14 AM

Quote:
Original post by Anonymous Poster
for a function with no input you use a 32 bit pointer
such as
void somefunct(void);
void *functptr=somefunct;

but if you need to use input and returns then you must make room for the extra memory needed

int somefunct(int x, char y, float z);
int (*functptr)(int,char,float)=somefunct;

it makes space for 17 bytes = sizeof(int)+sizeof((void *))+sizeof(int)+sizeof(char)+sizeof(float)

typedef struct STUFFSTRUCT{
int (*somefunct)(int,char,float);
} STUFFSTRUCT;

STUFFSTRUCT stuff;
STUFFSTRUCT *stuffptr;

stuffptr->somefunct=somefunct;
int myint=stuffptr->somefunct(1,*"a",3.0f);

Huh? You're wrong on just about every point there.

A pointer to a function is typically the same size as the address size of the system, i.e. 32 bits of a 32-bit architecture. This does not change regardless of the arguments and return type of the function (but will change if you're talking C++ and pointers to members).

If you have a function with no parameters and no return type then you use a pointer to a function taking no parameters and returning void, not a void *. You should only really use void * if you need to be able to store arbitrary types in C.

And *"a" is really poor style. ever heard of 'a'?

Σnigma

#20 Anonymous Poster_Anonymous Poster_*   Guests   -  Reputation:

0Likes

Posted 28 February 2006 - 05:44 AM

all must bow down and worship the mastermind, for he is the chosen one!
always correct, always right, always so full of himself




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS