C - Typedef and function pointers

Started by
24 comments, last by Emmanuel Deloget 18 years, 1 month ago
Cool, thanks guys.
Advertisement
Quote:Original post by SiCrane
Then choose a different example to pick on.


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

"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
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.
If there was a better all-purpose language out there people would be using it. ^^
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?
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.
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
- John
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);

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
all must bow down and worship the mastermind, for he is the chosen one!
always correct, always right, always so full of himself

This topic is closed to new replies.

Advertisement