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?

Bite Bytes
  • 1,455
  • 8
  • 24
  • Take a look at [this answer](https://stackoverflow.com/a/30345939/4265352). It explains how to read the C variables and types declarations. – axiac Jul 19 '17 at 12:08
  • The typedef is immaterial. That's how you define variables of the type. – StoryTeller - Unslander Monica Jul 19 '17 at 12:08
  • 1
    @StoryTeller this is not an exact duplicate, it asks about the declaration syntax for function pointers, not about *typedef* declarations. –  Jul 19 '17 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. – StoryTeller - Unslander Monica Jul 19 '17 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*". –  Jul 19 '17 at 12:20
  • @FelixPalmen - Let's not act coy. This does not answer what the OP isn't clear about. – StoryTeller - Unslander Monica Jul 19 '17 at 12:21
  • @StoryTeller I suggest to re-read the question. It starts with "*If I wanted to declare a `typedef`*". This question is about the syntax of typedefs. I wouldn't be surprised to see a duplicate at all, as it's a very basic question. Still, the linked question is not a duplicate. –  Jul 19 '17 at 12:22
  • @FelixPalmen - I suggest you re-read the question yourself. You didn't focus on the OP's actual confusion which is why his attempt failed. Your answer only adds it as an afterthought. Instead you seem adamant to focus on a superficial difference to keep this question open. Note the close bar also reads "**This question already has an answer here:**". No need for a 100% exact dupe. But this arguing is pointless. You voted to re-open, let's see if more in the community agree. – StoryTeller - Unslander Monica Jul 19 '17 at 12:25
  • @bite-bites if you put `typedef` in front of a variable declaration it turns into a type declaration. The name of the variable becomes the name of a new type. – axiac Jul 19 '17 at 12:28
  • @StoryTeller this is not about the question being opened or closed, I don't care. The issue is that the duplicate doesn't answer the question. The second one is slightly better, as one of the answers mentions what is the key issue here, still not a perfect choice. –  Jul 19 '17 at 12:48
  • @FelixPalmen - It does answer the core issue. The chasm between how you and I see the subject of the question is obviously too deep to bridge. But I re-iterate, if 4 other people (or one C gold badge person) agree with you, this will re-open. Trust the process. – StoryTeller - Unslander Monica Jul 19 '17 at 12:52
  • @StoryTeller if you look at the comment posted below my answer, it is even more clear that this question was about the syntax of `typedef`. You can correct that mistake or leave it. This question isn't important enough to get a lot of reviews, it would just be nice not to have misleading duplicates. –  Jul 19 '17 at 12:56
  • @StoryTeller the process is not really the best, some people got their reputations by giving only _opinions_, in the old days. If the same rules of today applied to them, their accounts would be deleted, and not given golden badges. – Bite Bytes Jul 19 '17 at 12:57
  • @BiteBytes - How about you clarify what it is that isn't clear for you. You didn't accept Felix's answer, so it obviously doesn't answer your question. And if the question is about why the function pointer syntax is weird, there are two dupes. Read them carefully instead of disparaging them. – StoryTeller - Unslander Monica Jul 19 '17 at 12:59

3 Answers3

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.

  • 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`. – Bite Bytes Jul 19 '17 at 12:32
  • @BiteBytes yes. PeterJ's answer shows how this applies to typedef'ing an array type. –  Jul 19 '17 at 12:43
  • 2
    @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. – John Bode Jul 19 '17 at 12:49
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.

Embedded C
  • 1,448
  • 3
  • 16
  • 29
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];
0___________
  • 60,014
  • 4
  • 34
  • 74