Disclaimer: First, I'm aware that this is C++ code and this is a C# forum, I'm not all that familiar with C++ and was hoping someone here could explain the example below in C#.
I was browsing the very complex source code of the open source project MySQL and saw this bit of code (see attachment for full file, original extension was cc, not cpp):
/* A structure which contains information on the commands this program
can understand. */
typedef struct {
const char *name; /* User printable name of the function. */
char cmd_char; /* msql command character */
int (*func)(String *str,char *); /* Function to call to do the job. */
bool takes_params; /* Max parameters for command */
const char *doc; /* Documentation for this function. */
} COMMANDS;
I understand everything except for line 7:
int (*func)(String *str,char *); /* Function to call to do the job. */
The code moves on immediately after in this type of fashion:
static COMMANDS commands[] = {
{ "?", '?', com_help, 1, "Synonym for `help'." },
{ "clear", 'c', com_clear, 0, "Clear command."},
{ "connect",'r', com_connect,1,
/* SNIPPED FOR BREVITY */
{ "lock tables", 0, 0, 0, ""},
{ "unlock tables", 0, 0, 0, ""},
/* generated 2006-12-28. Refresh occasionally from lexer. */
{ "ACTION", 0, 0, 0, ""},
{ "ADD", 0, 0, 0, ""},
{ "AFTER", 0, 0, 0, ""},
{ "AGAINST", 0, 0, 0, ""},
{ "AGGREGATE", 0, 0, 0, ""},
{ "ALL", 0, 0, 0, ""},
{ "ALGORITHM", 0, 0, 0, ""},
{ "ALTER", 0, 0, 0, ""},
{ "ANALYZE", 0, 0, 0, ""},
{ "AND", 0, 0, 0, ""},
{ "ANY", 0, 0, 0, ""},
{ "AS", 0, 0, 0, ""},
{ "ASC", 0, 0, 0, ""},
/* ETC... */
{ "WEEKDAY", 0, 0, 0, ""},
{ "WEEKOFYEAR", 0, 0, 0, ""},
{ "WITHIN", 0, 0, 0, ""},
{ "X", 0, 0, 0, ""},
{ "Y", 0, 0, 0, ""},
{ "YEARWEEK", 0, 0, 0, ""},
/* end sentinel */
{ (char *)NULL, 0, 0, 0, ""}
};
Even though I don't understand line 7, why would anyone waste time creating a struct like this if everything (seriously over 100 lines) all contain 99% of the same data. Am I missing something? Can anyone shed any light here, or even throw out a guess?