By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,364 Members | 1,734 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,364 IT Pros & Developers. It's quick & easy.

typedef problem

P: n/a
Hi all,
I am new to C programming. I have seen in many Source
files a declaration as follows,

typedef void iamafresher(int x, int y);

why they use typedef with function.

Please help,

Divya
Nov 14 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hi.

I guess what you saw was something like this:
typedef void (*iamafresher)(int x, int y);

This is a function type. The advantage of a function type is that you
can store addresses to functions in pointers and use them f.eks in a
function.

One thing, if you're new to c. Learn about pointers before you look
into this.

void myfunc(int x, int y, iamafresher callbackfunc)
{
callbackfunc(x, y);
}

void tmpfunc(int x, int y)
{
printf("The numbers are: %i and %i\n", x, y);
}

int main(void)
{
myfunc(10, 20, tmpfunc);
}

this example will make printf print out the numbers passed to myfunc.

--
bjrnove

Nov 14 '05 #2

P: n/a
bjrnove wrote:

Hi.

I guess what you saw was something like this:
typedef void (*iamafresher)(int x, int y);

This is a function type.


No. The type is pointer to a function returning void.
Expressions of function type are converted to pointers
by every operator except & and sizeof.
The sizeof operator is not defined for function type operands.

The three major categories of types are
function types, incomplete types and object types.

--
pete
Nov 14 '05 #3

P: n/a
On Thu, 17 Mar 2005 00:18:42 -0800, foodic wrote:
Hi all,
I am new to C programming. I have seen in many Source
files a declaration as follows,

typedef void iamafresher(int x, int y);

why they use typedef with function.


Functions have types so you can typedef them. The example above defines
iamafresher as a type name representing the type "function taking 2 int
arguments and returning void". You can use it in function declarations
such as

iamafresher myfunc;

which would be equivalent to

void myfunc(int, int);

but you can't use it is the corresponding function definition e.g.

void myfunc(iny x, int y)
{
/* function body */
}

can't be written using iamafresher. Function typedefs aren't particularly
helpful in most cases so while it is valid I'm surprised that you have
seen it in "many" source files.

Lawrence
Nov 14 '05 #4

P: n/a
"foodic" <di******************@yahoo.co.in> wrote in message
Hi all,
I am new to C programming. I have seen in many Source
files a declaration as follows,

typedef void iamafresher(int x, int y);

why they use typedef with function.


Beginners often use typedef too much. The point with typedef,
is mainly to increase the readability of your code. A typedef
don't define a new type, it's just an alias.

Typdef's can be overdone! This may be such a case... because

struct foo1
{
void (*iamaf) (int x, int y);
};

and

struct foo1
{
iamafresher *iamaf;
};

is different ways to say the _same_ thing. Experienced C
programmers prefer to declare struct foo1 the first way,
because that doesn't hide the fact that 'iamaf' is a
function.

The declaration of the signal function in the standard, is
quite complex:

void (*signal (int sig, void (*func) (int sig))) (int sig);

So what does this really mean? Well, with a typedef
it's much easier to understand:

typedef void signal_handler(int);

signal_handler *signal(int sig, signal_handler *func);

:-)

--
Tor <torust AT online DOT no>
"To this day, many C programmers believe that 'strong typing' just means
pounding extra hard on the keyboard". PvdL
Nov 14 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.