1

If I wanted to declare a typedef for a certain type, I would go for a syntax like this one, in this example:

typedef int INT

But when I wanted to create a typedef for a function, I was expecting the following syntax to be the one:

typedef void (*)(int, char) myfunc;

Instead the correct one is:

typedef void (*myfunc)(int, char);

So why the first one is not correct?

14
  • Take a look at this answer. It explains how to read the C variables and types declarations. Commented Jul 19, 2017 at 12:08
  • The typedef is immaterial. That's how you define variables of the type. Commented Jul 19, 2017 at 12:08
  • 1
    @StoryTeller this is not an exact duplicate, it asks about the declaration syntax for function pointers, not about typedef declarations. Commented Jul 19, 2017 at 12:17
  • @FelixPalmen - The syntax for a typedef is exactly the same as for a variable by definition. The reason for the variable syntax is in the dupe. Hanging on to the typedef is a nit-pick to keep an already answered question open. Commented Jul 19, 2017 at 12:19
  • @StoryTeller "The syntax for a typedef is exactly the same as for a variable by definition" <- and this is an answer to this question. No, not a "nitpick". Commented Jul 19, 2017 at 12:20

3 Answers 3

6

A typedef looks exactly like a "normal" declaration. If you declare a function pointer, it would look like this:

void (*myfunc)(int, char);

So, the typedef looks the same, with the only difference that the declared identifier doesn't refer to an object of the type, but to a type alias name instead:

typedef void (*myfunc)(int, char);

As for the "why?" -- Well, because the language is designed that way, but I guess once you understand how it works, it's arguably the easiest way not to introduce a different syntax for typedef declarations. This design is following the principle of least surprise, treating typedef declarations somehow differently would be needlessly complicated.

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

3 Comments

So, the rule is not the following: If I have a type called Type I can create a typedef called Name using typedef Type Name. But the rule instead, is: If have a declaration like this declarator name I can turn name to typedef by forwarding the delcarator by the keyword typedef.
@BiteBytes yes. PeterJ's answer shows how this applies to typedef'ing an array type.
@BiteBytes: Syntactically speaking, typedef is treated as a storage class specifier like auto or extern, even though it has a different meaning. So yes, the form of a typedef is the form of a regular declaration, just with the typedef keyword prepended to it.
0

The basic way to create function pointer is void (*myfunc)(int, char);

So to create a new type that is a function pointer which returns void and get int and char as argument is typedef void (*myfunc)(int, char);

that is why the first one is incorrect

There is no need of adding (*) before function pointer.

Comments

0

But when I wanted to create a typedef for a function, I was expecting the following syntax to be the one:

typedef void (*)(int, char) myfunc;

You can of course write you own compiler of your own language.

But C syntax is like this.

Here is the another example:

typedef char foo[10];

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.