typedef problem

F

foodic

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
 
B

bjrnove

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.
 
P

pete

bjrnove said:
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.
 
L

Lawrence Kirby

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
 
T

Tor Rustad

foodic said:
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);

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,891
Members
47,423
Latest member
henerygril

Latest Threads

Top