Solved

Question on function typedef?

Posted on 2002-03-13
4
328 Views
Last Modified: 2010-04-15
I am looking at a Richard Stevens book to help with a program i am writing.
the code i was looking at contained this:
----------
typedef  int  Myfunc(const char *, const struct stat *, int);
               /* function type that's called for each filename */
------------

what is this for?  i have not seen this before. is it just to simplify
passing a function and args to another function?
there is an function in the code myfunc().
i can post more of the code if needed.


Thanks
Jim
0
Comment
Question by:neptuna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 6862324
This statement is creating a "function pointer type" and basically makes code more "readable".

0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 6862344
BTW, "qsort()" is a nice example for this:

The "normal" declaration is

void qsort( void *base, size_t num, size_t width, int (__cdecl *compare )(const void *elem1, const void *elem2 ) );

Using the "typedef", you could rewrite it as

typedef int (__cdecl *COMPARE_PROC )(const void *elem1, const void *elem2 );

void qsort( void *base, size_t num, size_t width,  COMPARE_PROC compare);


where "COMPARE_PROC" is a type that represents a pointer to a function which takes the above argument list and returns an "int"
0
 

Author Comment

by:neptuna
ID: 6864695
so basically it is saving us some typing ?
0
 
LVL 86

Expert Comment

by:jkr
ID: 6864711
>>so basically it is saving us some typing ?

Yes. And it makes using function pointers more typesafe - just imagine the above "qsort()" would be called using a function with a different signature, e.g.

int __cdecl MyCompare(const int elem1, const int elem2 );

The result would be a crash...
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question