4

I want to declare a pointer type which point to a function, so I try:

typedef void (*print)(void); works perfect

void (*print)(void); p is a ponter variable , not a type.

typedef (void) (*print)(void); error expected identifier or ‘(’ before ‘void’

typedef void (*)(void) Print;

error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘_ attribute _’ before ‘Print’ .

My question is:

  1. Do I have to use typedef to declare a function pointer type ?

  2. Why typedef (void) (*print)(void); is wrong ? what () means here?

  3. Why I can't write in this way:typedef void (*)(void) Print ?

4
  • You don't need typedef to declare a function pointer typer. Commented Sep 5, 2013 at 14:20
  • How to do it ? post the idea ,please. Commented Sep 5, 2013 at 14:22
  • 2
    All your questions can pretty much be answered with "because the language designers decided it that way" Commented Sep 5, 2013 at 14:26
  • In an answer to another question, I tried to explain how to declare a function pointer. See stackoverflow.com/a/6905987/396551 Commented Sep 5, 2013 at 14:46

2 Answers 2

10

The correct way is:

typedef void (*print_function_ptr)(void)

and its usage for variable/parameter declaration is:

print_function_ptr p;
  1. You don't need a typedef to declare a variable. You can directly write void (*p)(void) to declare a variable p pointing to a function taking void and returning void. However to declare a type alias / name for a pointer to function, typedef is the tool.

  2. It does not mean anything it is not a valid C syntax.

  3. Because it is not how C works. Typedefs in C mimics how variables are declared or defined.

Sign up to request clarification or add additional context in comments.

4 Comments

I hope declare a function pointer type , not a variable, is there a way to do this without typedef?
@LidongGuo: what do you mean by 'declare a function pointer type'? You can declare an object that is of type 'pointer to function' without using a typedef. There is no way other than using typedef to create a type name that you can use subsequently.
I hope to declare a type like a struct name, then I can use it declare lots of function pointer'
@LidongGuo then this answer shows you how to do that. After the typedef, you can just do this to have 5 function pointers with the same signature: print_function_ptr p1, p2, p3, p4, p5;
3
  1. No, you don't have to use a typedef to create an object of type 'pointer to function':

    void somefunc(void (*pointer)(void))
    {
        (*pointer)();
        pointer();
    }
    

    However, there is no way to create a name for a type other than by using a typedef. I suppose you could indulge in macro hackery to generate a 'pointer to function', but after the macro is expanded, you'd have a 'pointer to function' written out:

    #define PTR_FUNC(func) void (*func)(void)
    void somefunc(PTR_FUNC(pointer)) { ... }
    
  2. The (void) notation as the type name is wrong. You don''t write: (int) x; and expect to declare a variable x -— it is a type cast. Same with the notation you're using in your typedef.

  3. You can't write typedef void (*)(void) Print; because it is not an allowed C syntax. You also can't write typedef [32] int name; either — it isn't valid C syntax.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.