Explain typedef for function used in qsort library
Asked Answered
C

3

2

I am using qsort library function to sort an array of structure elements, while searching on the Internet I found a resource: INFO: Sorting Structures with the C qsort() Function @ support.microsoft.

I understand that qsort function requires to be typecast by generic pointers.

However I am not able to get this line:

typedef int (*compfn) (const void*, const void*);

Which has been declared, and its subsequent call:

qsort((void *) &array,              // Beginning address of array
      10,                           // Number of elements in array
      sizeof(struct animal),        // Size of each element
      (compfn)compare               // Pointer to compare function
 );
  1. How is typedef behaving, I mean what exactly have we typedeffed int (*compfn) or int (compfn)?
  2. If the former, then shouldn't the call be (*compfn)?
Cates answered 22/7, 2013 at 8:9 Comment(0)
B
8

Syntax:

typedef  int (*compfn)  (const void*, const void*);
  ^      ^       ^            ^          ^
  | return type  |               arguments type
  |             new type name 
  defining new type

compfn is a new user defined type defined by typedef keyword,

So, you have exactly typedefded int (*)(const void*, const void*); to comfn using the syntax I described above.

A declaration:

 compfn  fun; // same as: int (*fun)  (const void*, const void*);

means fun is a function pointer that takes two arguments of const void* types and returns int.

Suppose you have a function like:

int xyz  (const void*, const void*);    

then you can assign xyz address to fun.

fun = &xyz; 

At calling qsort():

In expression (compfn)compare, you are typecasting a function compare to (compfn) type function.

A doubt:

shouldn't the call be (*compfn).

No, its type name not function name.

Note: if you just writing int (*compfn) (const void*, const void*); without typedef then comfn will be a pointer to a function that returns int and take two arguments of type const void*

Bermejo answered 22/7, 2013 at 8:13 Comment(7)
Thanks! But,please explain this code "typedef int (*compfn)" as well .What exactly have we "typedeffed", int (*compfn) or int (compfn).Cates
@DrefD typedef int (*compfn) is not correct, typedeffed also error . int (*compfn) should be int *compfnBermejo
@DrefD got your confusion now, see what I describe in syntax of define a new type, a type for function. What partial things you are asking about is not valid.Bermejo
@DrefD you have exactly typedefded int (*)(const void*, const void*); to comfn useing the syntax I described above.Bermejo
@GrijeshChauhan, please see ideone.com/6XqTrN in response to "int (*compfn) should be int *compfn". I guess the latter isn't correct, PS: your last note clears some of my doubt!Cates
Is what I am thinking correct? According to: codeproject.com/Articles/7042/… and GrijeshChauhan, we have "typedeffed" the entire expression to compfn, thus compfn in itself refers to int ( * )(const void*, const void*);Cates
@DrefD I saw the link-1 of ideone I didn't find any "int (*compfn)Bermejo
L
3

The typedef declaration creates an alias for a specific type. This means it can be used as any other type in declarations and definitions.

So if you have e.g.

typedef int (*compfn)(const void*, const void*);

Then you can declare a variable or argument using only compfn instead of the whole function pointer declaration. E.g. these two declarations are equal:

compfn function_pointer_1;
int (*function_pointer_2)(const void*, const void*);

Both creates a function pointer variable, and the only difference is the name of the variable name.

Using typedef is common when you have long and/or complicated declarations, to easy both your writing of such declarations and to make it easier to read.

Luca answered 22/7, 2013 at 8:16 Comment(0)
S
0

It is a type of a function pointer. The function which is being pointed to returns int and accepts two const void* parameters.

Serriform answered 22/7, 2013 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.