Function pointer syntax help
-
Can someone tell me what's wrong with this code:
#include typedef void (*VoidFunctionPtr)(void);
void FunctionA()
{
printf("You are in Function A.\n");
}void FunctionB()
{
printf("You are in Function B.\n");
}int main(void)
{
VoidFunctionPtr fp = FunctionA;
fp();
}In main(), the declaration of variable "fp" throws a C2275 error: "'VoidFunctionPtr': illegal use of this type as an expression." I don't get it. I have cross-referenced several books and internet articles. All show the same syntax. What am I missing?
-
Can someone tell me what's wrong with this code:
#include typedef void (*VoidFunctionPtr)(void);
void FunctionA()
{
printf("You are in Function A.\n");
}void FunctionB()
{
printf("You are in Function B.\n");
}int main(void)
{
VoidFunctionPtr fp = FunctionA;
fp();
}In main(), the declaration of variable "fp" throws a C2275 error: "'VoidFunctionPtr': illegal use of this type as an expression." I don't get it. I have cross-referenced several books and internet articles. All show the same syntax. What am I missing?
Just tested your code with VC2010 express: compiles and runs Ok. Which compiler do you use? cheers, AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
-
Can someone tell me what's wrong with this code:
#include typedef void (*VoidFunctionPtr)(void);
void FunctionA()
{
printf("You are in Function A.\n");
}void FunctionB()
{
printf("You are in Function B.\n");
}int main(void)
{
VoidFunctionPtr fp = FunctionA;
fp();
}In main(), the declaration of variable "fp" throws a C2275 error: "'VoidFunctionPtr': illegal use of this type as an expression." I don't get it. I have cross-referenced several books and internet articles. All show the same syntax. What am I missing?
Tested and works in VS6.0, 2003, 2010. What compiler are you using? Is that the only piece of code you have? can you create new project and have only this code in it? What happens if you do that?
Yusuf May I help you?
-
Just tested your code with VC2010 express: compiles and runs Ok. Which compiler do you use? cheers, AR
When the wise (person) points at the moon the fool looks at the finger (Chinese proverb)
I am using VS2010 Premium. I come from a C++ background but I need to learn what the difference is between that and standard C. To facilitate that goal I created an empty C++ project and then set the following Project Properties to disable C++ components: Language -> Disable Language Extensions = YES (command line switch /Za) Advanced -> Compile As = Compile As C Code (command line switch /TC)
-
Can someone tell me what's wrong with this code:
#include typedef void (*VoidFunctionPtr)(void);
void FunctionA()
{
printf("You are in Function A.\n");
}void FunctionB()
{
printf("You are in Function B.\n");
}int main(void)
{
VoidFunctionPtr fp = FunctionA;
fp();
}In main(), the declaration of variable "fp" throws a C2275 error: "'VoidFunctionPtr': illegal use of this type as an expression." I don't get it. I have cross-referenced several books and internet articles. All show the same syntax. What am I missing?
I found the problem. I only posted a limited version of the code to focus the direction of the inquiry. In the context of the full code, what is going on is that the compiler expects all variables to be declared at the beginning of the function. Original code:
#include typedef void (*VoidFunctionPtr)(void);
void FunctionA(void)
{
printf("You are in Function A.\n");
}int main(void)
{
printf("Here is a program that does something.\n");
VoidFunctionPtr fp = FunctionA; // FAILS because variables must be declared first.
fp();
}Revised code:
#include typedef void (*VoidFunctionPtr)(void);
void FunctionA(void)
{
printf("You are in Function A.\n");
}int main(void)
{
VoidFunctionPtr fp = FunctionA; // MOVED variable declaration to first line.printf("Here is a program that does something.\\n"); fp();
}
I'm sure this wouldn't happen in a C++ project, but I am using compiler settings to force Standard C compilation. I come from a C++ background but I'm trying to learn the differences between C++ and standard C. I bought a book on standard C, but it references the "C99" standard, which apparently allows variables to be defined anywhere in a function. I guess the MS C compiler works on an older standard?
-
I found the problem. I only posted a limited version of the code to focus the direction of the inquiry. In the context of the full code, what is going on is that the compiler expects all variables to be declared at the beginning of the function. Original code:
#include typedef void (*VoidFunctionPtr)(void);
void FunctionA(void)
{
printf("You are in Function A.\n");
}int main(void)
{
printf("Here is a program that does something.\n");
VoidFunctionPtr fp = FunctionA; // FAILS because variables must be declared first.
fp();
}Revised code:
#include typedef void (*VoidFunctionPtr)(void);
void FunctionA(void)
{
printf("You are in Function A.\n");
}int main(void)
{
VoidFunctionPtr fp = FunctionA; // MOVED variable declaration to first line.printf("Here is a program that does something.\\n"); fp();
}
I'm sure this wouldn't happen in a C++ project, but I am using compiler settings to force Standard C compilation. I come from a C++ background but I'm trying to learn the differences between C++ and standard C. I bought a book on standard C, but it references the "C99" standard, which apparently allows variables to be defined anywhere in a function. I guess the MS C compiler works on an older standard?
When writing C, you should probably try to declare all variables at the beginning to make sure you have compatibility with all compilers since that wasn't allowed until later on (2000?). http://en.wikipedia.org/wiki/ANSI_C[^]
-
When writing C, you should probably try to declare all variables at the beginning to make sure you have compatibility with all compilers since that wasn't allowed until later on (2000?). http://en.wikipedia.org/wiki/ANSI_C[^]
-
I found the problem. I only posted a limited version of the code to focus the direction of the inquiry. In the context of the full code, what is going on is that the compiler expects all variables to be declared at the beginning of the function. Original code:
#include typedef void (*VoidFunctionPtr)(void);
void FunctionA(void)
{
printf("You are in Function A.\n");
}int main(void)
{
printf("Here is a program that does something.\n");
VoidFunctionPtr fp = FunctionA; // FAILS because variables must be declared first.
fp();
}Revised code:
#include typedef void (*VoidFunctionPtr)(void);
void FunctionA(void)
{
printf("You are in Function A.\n");
}int main(void)
{
VoidFunctionPtr fp = FunctionA; // MOVED variable declaration to first line.printf("Here is a program that does something.\\n"); fp();
}
I'm sure this wouldn't happen in a C++ project, but I am using compiler settings to force Standard C compilation. I come from a C++ background but I'm trying to learn the differences between C++ and standard C. I bought a book on standard C, but it references the "C99" standard, which apparently allows variables to be defined anywhere in a function. I guess the MS C compiler works on an older standard?
MS isn't concerned with keeping up with the latest C specs, so you'll need to avoid C99-specific features.
--Mike-- Dunder-Mifflin, this is Pam.
-
MS isn't concerned with keeping up with the latest C specs, so you'll need to avoid C99-specific features.
--Mike-- Dunder-Mifflin, this is Pam.
-
Yeah, I figured that out. Do you happen to know what (if any) standard they do conform to? I know there are several through the years.
If you look at the link I provided before, it states that they implemented "Microsoft Visual C++ (C90. A few features of C99)". I'm guessing you would have to dig deep to figure out what they did and didn't implement.
-
If you look at the link I provided before, it states that they implemented "Microsoft Visual C++ (C90. A few features of C99)". I'm guessing you would have to dig deep to figure out what they did and didn't implement.
-
I found the problem. I only posted a limited version of the code to focus the direction of the inquiry. In the context of the full code, what is going on is that the compiler expects all variables to be declared at the beginning of the function. Original code:
#include typedef void (*VoidFunctionPtr)(void);
void FunctionA(void)
{
printf("You are in Function A.\n");
}int main(void)
{
printf("Here is a program that does something.\n");
VoidFunctionPtr fp = FunctionA; // FAILS because variables must be declared first.
fp();
}Revised code:
#include typedef void (*VoidFunctionPtr)(void);
void FunctionA(void)
{
printf("You are in Function A.\n");
}int main(void)
{
VoidFunctionPtr fp = FunctionA; // MOVED variable declaration to first line.printf("Here is a program that does something.\\n"); fp();
}
I'm sure this wouldn't happen in a C++ project, but I am using compiler settings to force Standard C compilation. I come from a C++ background but I'm trying to learn the differences between C++ and standard C. I bought a book on standard C, but it references the "C99" standard, which apparently allows variables to be defined anywhere in a function. I guess the MS C compiler works on an older standard?
Another difference that's easy for C++ devs to forget is the fact that C doesn't support parameters with defaults. Example: Valid in C++, not valid in C
int GetFoo(int HowMany=1){}
-
Another difference that's easy for C++ devs to forget is the fact that C doesn't support parameters with defaults. Example: Valid in C++, not valid in C
int GetFoo(int HowMany=1){}
-
That's OK by me. I don't recall ever using defaults... not since college 15 years ago anyway when some homework assignment was specifically targeted at it. I do a fair amount of work in C# too, which doesn't allow defaults, so I'm used to it.
I use C++ A LOT, so I do forget that one sometimes... and as you've noticed, depending on the compiler (and settings), it may or may not allow it... pain in the butt! X|