473,382 Members | 1,526 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,382 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 4410
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.