Solved

Question on function typedef?

Posted on 2002-03-13
4
313 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now