Solved

Question on function typedef?

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

777 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