I download a code example written in C, but don’t understand one instruction. And besides, w
hen i try to compile the code the compiler throws me an error just in the line that i don’t understand.
Code:
// Global vars
static int getting_text = 0;
static char *the_text; // Definition Part
static void (*text_entered)(); // Definition Part 2
// method
int add_text(unsigned char key)
{
char msg[] = "x";
int len;
if(!getting_text) return 0;
if(key==8) /* backspace */
{
len = strlen(the_text);
the_text[len-1] = '\0';
}
else if(key==13 || key==9) // cr or tab ends
{
getting_text = 0;
text_entered(the_text); // Execution Part
}
else
{
msg[0] = key;
strcat(the_text, msg);
}
glutPostRedisplay();
return 1;
}
The compiler throws me an error about there are too many arguments in the method’s calling. But i don’t if it’s a method the static void (*xxx)() or if other thing.
Thanks in advance.
EDIT: The following only applies to C++. Did you use g++ or some other C++ compiler instead of a C compiler?
text_entered is a function pointer to a function that doesn’t take any arguments, hence the error, because you’re passing it a character pointer. I assume it should change to,
This is of course assuming text_enterered actually gets set to a function that takes a char* argument and it isn’t just being called wrong.