By Jack Harvin


2010-11-28 04:50:25 8 Comments

I'm learning how to dynamically load DLL's but what I don't understand is this line

typedef void (*FunctionFunc)();

I have a few questions. If someone is able to answer them I would be grateful.

  1. Why is typedef used?
  2. The syntax looks odd; after void should there not be a function name or something? It looks like an anonymous function.
  3. Is a function pointer created to store the memory address of a function?

So I'm confused at the moment; can you clarify things for me?

6 comments

@alinsoar 2019-05-14 08:50:27

For general case of syntax you can look at annex A of the ANSI C standard.

In the Backus-Naur form from there, you can see that typedef has the type storage-class-specifier.

In the type declaration-specifiers you can see that you can mix many specifier types, the order of which does not matter.

For example, it is correct to say,

long typedef long a;

to define the type a as an alias for long long. So , to understand the typedef on the exhaustive use you need to consult some backus-naur form that defines the syntax (there are many correct grammars for ANSI C, not only that of ISO).

When you use typedef to define an alias for a function type you need to put the alias in the same place where you put the identifier of the function. In your case you define the type FunctionFunc as an alias for a pointer to function whose type checking is disabled at call and returning nothing.

@Jacob Relkin 2010-11-28 04:57:43

  1. typedef is used to alias types; in this case you're aliasing FunctionFunc to void(*)().

  2. Indeed the syntax does look odd, have a look at this:

    typedef   void      (*FunctionFunc)  ( );
    //         ^                ^         ^
    //     return type      type name  arguments
    
  3. No, this simply tells the compiler that the FunctionFunc type will be a function pointer, it doesn't define one, like this:

    FunctionFunc x;
    void doSomething() { printf("Hello there\n"); }
    x = &doSomething;
    
    x(); //prints "Hello there"
    

@Cheers and hth. - Alf 2010-11-28 05:00:59

typedef does not declare a new type. you can have many typedef-defined names of the same type, and they are not distinct (e.g. wrt. overloading of functions). there are some circumstances in which, with respect to how you can use the name, a typedef-defined name is not exactly equivalent to what it's defined as, but multiple typedef-defined names for the same, are equivalent.

@Jack Harvin 2010-11-28 05:09:06

Ah i get it now. Thanks.

@Jacob Relkin 2010-11-28 05:09:37

@Jack, You're welcome! :)

@Michael van der Westhuizen 2012-01-19 18:57:19

+1 for the answer to question 2 - very clear. The rest of the answers were clear too, but that one stands out for me :-)

@Halil Kaskavalci 2017-09-21 08:20:05

If you can use C++11 you may want to use std::function and using keyword.

using FunctionFunc = std::function<void(int arg1, std::string arg2)>;

@Top-Master 2019-03-11 04:25:15

without C++11 using keyword would be like typedef std::function<void(int, std::string)> FunctionFunc;, just in case someone wants another wrapper around functions without C++11

@Amjad 2017-01-11 22:24:22

#include <stdio.h>
#include <math.h>

/*
To define a new type name with typedef, follow these steps:
1. Write the statement as if a variable of the desired type were being declared.
2. Where the name of the declared variable would normally appear, substitute the new type name.
3. In front of everything, place the keyword typedef.
*/

// typedef a primitive data type
typedef double distance;

// typedef struct 
typedef struct{
    int x;
    int y;
} point;

//typedef an array 
typedef point points[100]; 

points ps = {0}; // ps is an array of 100 point 

// typedef a function
typedef distance (*distanceFun_p)(point,point) ; // TYPE_DEF distanceFun_p TO BE int (*distanceFun_p)(point,point)

// prototype a function     
distance findDistance(point, point);

int main(int argc, char const *argv[])
{
    // delcare a function pointer 
    distanceFun_p func_p;

    // initialize the function pointer with a function address
    func_p = findDistance;

    // initialize two point variables 
    point p1 = {0,0} , p2 = {1,1};

    // call the function through the pointer
    distance d = func_p(p1,p2);

    printf("the distance is %f\n", d );

    return 0;
}

distance findDistance(point p1, point p2)
{
distance xdiff =  p1.x - p2.x;
distance ydiff =  p1.y - p2.y;

return sqrt( (xdiff * xdiff) + (ydiff * ydiff) );
}

@Donna 2017-10-30 14:06:53

Under what circumstances is the '&' needed? For example, do 'func_p = &findDistance' and 'func_p = findDistance' work equally well?

@Amjad 2017-10-30 16:41:27

A function name is a pointer, so you do not need to use '&' with it. In other words, '&' is optional when function pointers are considered. However, for primitive data type you need to use the '&' to get its address.

@Donna 2017-10-31 17:39:34

It has just always struck as odd that the '&' can ever be optional. If a typedef is used to create a pointer to a primitive (i.e. typedef (*double) p, is the '&' optional?

@Amjad 2017-10-31 20:16:37

I think you wanted to type typedef double* p to define a pointer to a double. If you want to populate the p pointer from a primitive variable, you will need to use the '&'. A side note, we you *<pointer name> to dereference a pointer. The * is used in the function pointer to dereference the pointer (function name) and go to the block of memory where the function instructions are located.

@Ring Ø 2010-11-28 05:13:03

typedef is a language construct that associates a name to a type.
You use it the same way you would use the original type, for instance

  typedef int myinteger;
  typedef char *mystring;
  typedef void (*myfunc)();

using them like

  myinteger i;   // is equivalent to    int i;
  mystring s;    // is the same as      char *s;
  myfunc f;      // compile equally as  void (*f)();

As you can see, you could just replace the typedefed name with its definition given above.

The difficulty lies in the pointer to functions syntax and readability in C and C++, and the typedef can improve the readability of such declarations. However, the syntax is appropriate, since functions - unlike other simpler types - may have a return value and parameters, thus the sometimes lengthy and complex declaration of a pointer to function.

The readability may start to be really tricky with pointers to functions arrays, and some other even more indirect flavors.

To answer your three questions

  • Why is typedef used? To ease the reading of the code - especially for pointers to functions, or structure names.

  • The syntax looks odd (in the pointer to function declaration) That syntax is not obvious to read, at least when beginning. Using a typedef declaration instead eases the reading

  • Is a function pointer created to store the memory address of a function? Yes, a function pointer stores the address of a function. This has nothing to do with the typedef construct which only ease the writing/reading of a program ; the compiler just expands the typedef definition before compiling the actual code.

Example:

typedef int (*t_somefunc)(int,int);

int product(int u, int v) {
  return u*v;
}

t_somefunc afunc = &product;
...
int x2 = (*afunc)(123, 456); // call product() to calculate 123*456

@pranavk 2013-03-05 13:32:57

in the last example, wouldn't just 'square' refer to the same thing i.e pointer to the function instead of using &square.

@dchhetri 2013-05-03 03:56:35

Question, in your first typedef example you have of the form typedef type alias but with function pointers there only seems to be 2 arguments, typedef type. Is alias defaulted to the name specified in type argument?

@Jonathan Leffler 2013-05-03 03:58:00

@pranavk: Yes, square and &square (and, indeed, *square and **square) all refer to the same function pointer.

@Jonathan Leffler 2013-05-03 04:01:09

@user814628: It is not clear quite what you're asking. With typedef int newname, you are making newname into an alias for int. With typedef int (*func)(int), you are making func into an alias for int (*)(int) — a pointer to function taking an int argument and returning an int value.

@dchhetri 2013-05-03 05:07:45

I guess I'm just confused about the ordering. With typedef int (*func)(int), I understand that func is an alias, just a little confused because the alias is tangled with the type. Going by typedef int INT as an example I would be more of ease if typedef function pointer was of form typedef int(*function)(int) FUNC_1. That way I can see the type and alias in two separate token instead of being meshed into one.

@Dan 2013-12-04 23:47:57

The reason the it looks like that is that typedef is a storage class specifier, like extern or static. You can think of it changing the storage class to type definitions. source. Thinking of it this way makes the syntax easier to remember.

@Cheers and hth. - Alf 2015-03-24 11:16:20

Regarding "Is a function pointer created to store the memory address of a function? Yes, ", no, not in this code. Also, stating that the name introduced by a typedef is a "keyword" is incorrect. And regarding the evaluation "the syntax is appropriate", both the C and C++ language creators disagree, calling it a failed experiment.

@Jon Spencer 2017-03-28 20:32:23

While a function usually just decays to a function pointer, this is not always the case. It is safer to just always use the & rather than relying on the decay always occurring. (Or you could just wait until the compiler complains and then add it - up to you :-) )

@user2356685 2018-05-02 06:06:45

It would be nice to talk a little bit more about the semantics of a C-style function pointer and a C++ object-oriented function pointer, specifically about how much more memory it takes in C++ and typedef just doesn't work for OO function pointers.

@cmaster 2019-05-17 07:00:17

Note that a function pointer is not necessarily just an "address". What it actually is, depends on your platform. On some platforms, it's really just the address of the first byte of the functions' machine code, on other platforms it may be something different. On the PPC platform, for instance, a function pointer was actually a pair of addresses: One gave the start of the machine code, the other the associated globals table. So sizeof(void (*)()) == 2*sizeof(void*). Other platforms may use something even more strange - function pointers are just not the same as "normal" pointers!

@Lewis Chan 2019-08-16 08:48:41

Furthermore, google test writes typedef like this, very weird. @ring-Ø Could you please explain some ?

@Cheers and hth. - Alf 2010-11-28 04:58:34

Without the typedef word, in C++ the declaration would declare a variable FunctionFunc of type pointer to function of no arguments, returning void.

With the typedef it instead defines FunctionFunc as a name for that type.

@Mad Physicist 2015-04-07 20:51:42

This is a really good way to think about it. If you read the other answers carefully, they do not really address the OP's confusion about the entanglement of the alias and name. I learned something new from reading this post.

Related Questions

Sponsored Content

37 Answered Questions

21 Answered Questions

[SOLVED] Why should I use a pointer rather than the object itself?

  • 2014-03-03 11:54:16
  • gEdringer
  • 294559 View
  • 1521 Score
  • 21 Answer
  • Tags:   c++ pointers c++11

13 Answered Questions

[SOLVED] What is a smart pointer and when should I use one?

13 Answered Questions

[SOLVED] What is a typedef enum in Objective-C?

5 Answered Questions

[SOLVED] What is the difference between 'typedef' and 'using' in C++11?

11 Answered Questions

[SOLVED] How do function pointers in C work?

  • 2009-05-08 15:49:17
  • Yuval Adam
  • 767521 View
  • 1160 Score
  • 11 Answer
  • Tags:   c function-pointers

12 Answered Questions

[SOLVED] typedef struct vs struct definitions

  • 2009-11-04 17:21:57
  • user69514
  • 646555 View
  • 755 Score
  • 12 Answer
  • Tags:   c struct typedef

16 Answered Questions

[SOLVED] Is the practice of returning a C++ reference variable evil?

  • 2009-04-15 16:49:49
  • Nick Bolton
  • 276985 View
  • 322 Score
  • 16 Answer
  • Tags:   c++ reference c++-faq

8 Answered Questions

[SOLVED] Difference between 'struct' and 'typedef struct' in C++?

  • 2009-03-04 20:41:12
  • criddell
  • 489698 View
  • 794 Score
  • 8 Answer
  • Tags:   c++ struct typedef

9 Answered Questions

[SOLVED] typedef syntax with member function pointers

  • 2011-06-07 13:24:00
  • muxmux
  • 34322 View
  • 25 Score
  • 9 Answer
  • Tags:   c++ typedef

Sponsored Content