473,395 Members | 1,584 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

typedef problem

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
4 4411
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
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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Vu Pham | last post by:
I think this problem relates to either c or c++ ( but I am not sure which one ) so I post to both of these news group. I am sorry if I did something wrong here. Is there any difference between...
1
by: Alex Borghgraef | last post by:
Hi all, I'm trying to get the Limp (Large Image Manipulation Program) library to compile using gcc 3.2, and I've encountered some problems. One is that the library systematically refers to...
14
by: dreamcatcher | last post by:
I always have this idea that typedef a data type especially a structure is very convenient in coding, but my teacher insisted that I should use the full struct declaration and no further...
2
by: Neil McPhail | last post by:
I'm new to C (and Usenet) and have been using the dreaded Schildt's Complete C Reference. I appreciate this may not have been the best idea, so no need to point this out! On page 546 there is a...
2
by: Immo Birnbaum | last post by:
Hi, I'm trying to solve a programming lab assignment for my college C programming course, but as they taught us two semesters of Java before teaching us any C, I'm having problems with all the...
16
by: burn | last post by:
Hello, i am writing a program under linux in c and compile my code with make and gcc. Now i have 4 files: init.c/h and packets.c/h. Each header-file contains some: init.h: struct xyz {
2
by: Patrick Kowalzick | last post by:
Hello NG, sorry to bother again, but I am a lit surprised that I got no answer on my post (attached below). So I refined the code a little bit :-). If there is a typedefed class X inside a...
5
by: jimmy | last post by:
I am trying to simulate typedef template similar to the suggestion of Herb Sutter in the following article: http://www.gotw.ca/gotw/079.htm However when implementing typedef templates according...
15
by: Ian Bush | last post by:
Hi All, I'm a bit confused by the following which is causing one of our user's codes fail in compilation: typedef struct SctpDest_S; 1) Is this standard ? 2) If so ( or even if not so ! )...
12
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.