0

I have two possibly simple questions as far as the code below is concerned:

#include <windows.h>//IN and OUT prefixes are defined here

typedef int(__cdecl *FOO)(IN int input);
int my_int_func(IN int x) {
    return x;
}

int main() {
    int res = 0;
    FOO foo = &my_int_func;
    res = (*foo)(5);
    return 0;
}

The code actually works flawlessly. Here are the questions:

1) What does the IN prefix mean in the typedef row? The prefix is defined in windows.h header. The code hangs if the header is omitted.

2) If I change __cdecl calling convention to __stdcall, the compiler underlines the &my_int_func and outputs the error message "Error: a value of type "int (*)(int x)" cannot be used to initialize an entity of type "FOO." My question is why?

I use MS Visual Studio Ultimate 2013.

5
  • Is it C? Add the language tag. Commented Nov 5, 2014 at 10:09
  • Yes, it is. The code is built as a console application. Commented Nov 5, 2014 at 10:16
  • IN is just hint for a human reader, it expands to nothing. If you change __cdecl to __stdcall, define my_int_func __stdcall as well. Commented Nov 5, 2014 at 10:25
  • 1
    A SSCCE in the first questions. Great! Commented Nov 5, 2014 at 10:38
  • Look up the definitions of IN and OUT in that header Commented Nov 5, 2014 at 10:49

1 Answer 1

2
  1. IN and OUT are most likely indications for direction of parameter. They might serve as documentation for the user or 3rd party tools, or they could be hints to compiler to aid in optimization. In any case, those are not portable, and you must consider if they are worth using.

  2. Calling convention determines how function parameters are passed, how return value is passed, and where the cleanup happens after the function call is over. You obviously cannot mix different calling conventions, and that is what compiler is complaining about. Calling conventions are also not very portable, so you shouldn't specify it, unless you have a good reason to do so.

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

Comments

Your Answer

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