-1

I am trying to use typedef with a pointer to function, and it's behaving weirdly.

The following code works:

#include <stdio.h>
#include <stdlib.h>
int funct(int num)
{
    return 0;
}
typedef int(*func)(int num);
int main()
{
    if (1) { return 0; }
    func c = &funct;
    return 0;
}

But the following code (without the {}) doesn't:

#include <stdio.h>
#include <stdlib.h>
int funct(int num)
{
    return 0;
}
typedef int(*func)(int num);
int main()
{
    if (1) return 0; 
    func c = &funct;
    return 0;
}

It tells me it's an illegal use of the function. Why? Isn't it basically the same code?

EDIT: Errors-

Warning 4   warning C4047: '=' : 'int' differs in levels of indirection from 'int (__cdecl *)(int)' 
Error   1   error C2275: 'func' : illegal use of this type as an expression 
Error   2   error C2146: syntax error : missing ';' before identifier 'c'   
Error   3   error C2065: 'c' : undeclared identifier    

EDIT 2: I inserted the complete code. In the first example, I get no error after I build the code. In the second piece of code, I get all the errors mentioned above.

EDIT 3: I created a new file and tried the code and it works. I still have no idea what caused the error in the first place, so if someone could give me an idea that would be great.

ndi equals
  • 187
  • 1
  • 9
  • int funct(int num) { return 0; } – ndi equals Dec 30 '15 at 12:25
  • 6
    First snippet will not compile as no `;` after `return 0`. Second will compile. – haccks Dec 30 '15 at 12:26
  • 1
    In your first example, the line: `if (1) {return 0};` is giving an error – Ziezi Dec 30 '15 at 12:26
  • 6
    Hiding levels of pointer indirection behind `typedef`s is, at least from a maintenance perspective, hideous. Please consider `typedef int func(int num);` and then `func *c = &funct;`... (I'm not saying you should change your question, just consider this for your future programming style) – autistic Dec 30 '15 at 12:27
  • 2
    Please be careful to _copy and paste exactly the code that you have tried!_ The code that you have posted does not give the behavior you claim, and that makes it very difficult to help. – Thomas Padron-McCarthy Dec 30 '15 at 12:27
  • I added the full code and the errors. – ndi equals Dec 30 '15 at 12:33
  • With your latest changes, on my compiler ("gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4") both programs compile without errors. Which compiler are you using? – Thomas Padron-McCarthy Dec 30 '15 at 12:34
  • @ThomasPadron-McCarthy; You must get warning/error about not using `c` variable. – haccks Dec 30 '15 at 12:35
  • @haccks: Yes, a warning. No errors. – Thomas Padron-McCarthy Dec 30 '15 at 12:36
  • My guess, while I am trying to find the charger to my Windows computer so I can try it in Visual Studio, is that you are _not_ compiling the second program you have shown, but that there is some mistake, and you are compiling some other version of it. – Thomas Padron-McCarthy Dec 30 '15 at 12:38
  • @ThomasPadron-McCarthy As long as the warnings are there, people should treat them as Errors, which means that we shouldn't compile that code with them – Michi Dec 30 '15 at 12:47
  • @Allen This is a terrible code and a terrible example. Update your Question with a code which actually does compile, with less problems. – Michi Dec 30 '15 at 12:51
  • 1
    @Michi In production code, yes, but this is obviously an MCVE test case - we *expect* a warning that `c` isn't used because we *know* it isn't being used. That isn't the point. – J... Dec 30 '15 at 12:51
  • @J... YES, but this is not our problem to think or to judge things here. The OP should ask that Question in a manner which we do understand he's point. – Michi Dec 30 '15 at 12:54
  • @Michi I was replying to your broadbrush statement that we should treat warnings as errors. As for the code not compiling - *that's why it's a question*. If OP had working code that compiled without errors then they would not have a question to ask. – J... Dec 30 '15 at 12:57
  • @Michi Unfortunately some warnings are redundant, especially when working with the binary operators in the hands of an expert who is well aware of what precedence the operators have, reads the warnings and rolls their eyes because they already knew... – autistic Dec 30 '15 at 13:09
  • @Michi For example, `if ((x & x + 1) == 0) { puts("x is a power of two"); }` will emit a warning treated as an error, despite the fact that it's perfectly compliant and well defined code... The compiler will require that superfluous brackets be inserted. – autistic Dec 30 '15 at 13:11
  • @Seb I don't understand why you mean. Is [This](http://ideone.com/EF5dZc) what you meant ? – Michi Dec 30 '15 at 14:30
  • @Michi `x` needs to be declared as some kind of `unsigned` type, and initialised... – autistic Dec 31 '15 at 03:25

1 Answers1

4

The error messages are a big hint. They look an awful lot like MSVC++ error numbers, don't they? Are you by chance using a C++ compiler to compile C code? I'll spare you the lecture, for now. Please use a C compiler to compile your C code. If you need to use that C code in a C++ project, you can use your linker to link the compiled-as-C module as you're compiling your C++ code, similar to how Microsoft recommends linking ws2_32.lib when you wish to use winsock.

I understand that MSVC++ has an option to "compile as C", but Microsoft's C compiler is really outdated; it only supports C89, which means you can't mix declarations with code, at least not until VS2013.

In VS2013 there is a bug which fits your problem. If you're using this IDE, I recommend upgrading (only slightly) to fix that bug... Mind you, MSVC++ 2013 still doesn't fully support C99 :( so maybe it's time to upgrade to LLVM/Clang in VS.

Community
  • 1
  • 1
autistic
  • 1
  • 3
  • 35
  • 80
  • I think is time to move on from Microsoft's compiler :) – Michi Dec 30 '15 at 12:50
  • I tried it in Visual Studio 2013, and it worked for the OP when trying again with a new file, so this does not seems to be the problem. The OP probably compiled the wrong file, as sometimes happens. – Thomas Padron-McCarthy Dec 30 '15 at 12:50
  • Shall I launch into a rant about "monopoly position = no motivation to innovate/improve"? No, probably not... :-) – Bob Jarvis - Слава Україні Dec 30 '15 at 12:50
  • @ThomasPadron-McCarthy If you read the page linked to by the "there is a bug" text, you'll find out that the Spring edition of VS2013 is patched... – autistic Dec 30 '15 at 12:51
  • @Seb: The VS2013 I tried it in says "Update 2", so yes, it's probably patched. But if that was the explanation, it seems a bit strange that the OP got it to work with a new file. – Thomas Padron-McCarthy Dec 30 '15 at 12:53
  • @ThomasPadron-McCarthy It's likely that he forgot to toggle "Compile as C" for the new file, so he's using a C++ compiler to compile C code. :'( – autistic Dec 30 '15 at 12:54
  • When trying it in my VS2013 it worked both as C and C++. But if the OP has the unpatched version, it indeed seems possible that this is what happened. You get my vote. – Thomas Padron-McCarthy Dec 30 '15 at 13:06