Solved

Question on function typedef?

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.

867 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

16 Experts available now in Live!

Get 1:1 Help Now