Код
const CHAR *fna(void); // Example function prototype
static void process_read(const CHAR *buf)
{
CHAR *cmdptr;
UCHAR offset;
const CHAR *replyptr;
static const CHAR read_str[] =
"0SV 0SN 0MO 0WF 0MT 0MP 0SW 1SP 1VO 1CC 1CA 1CB
1ST 1MF 1CL 1SZ 1SS 1AZ 1AS 1BZ 1BS 1VZ 1VS 1MZ
1MS 2SP 2VO 2CC 2CA 2CB 2ST 2MF 2CL 2SZ 2SS
2AZ 2AS 2BZ 2BS 2VZ 2VS 2MZ 2MS ";
static const CHAR *
(* const readfns[sizeof(read_str)/4])(void) =
{
una, fnb, fnc, ...
};
cmdptr = strstr(read_str, buf);
if (cmdptr != NULL)
{
/*
* cmdptr points to the valid command, so compute offset,
* in order to get entry into function jump table
*/
offset = (cmdptr - read_str) / 4;
/* Call function and get pointer to reply*/
replyptr = (*readfns[offset])();
/* rest of the code goes here */
}
}
static void process_read(const CHAR *buf)
{
CHAR *cmdptr;
UCHAR offset;
const CHAR *replyptr;
static const CHAR read_str[] =
"0SV 0SN 0MO 0WF 0MT 0MP 0SW 1SP 1VO 1CC 1CA 1CB
1ST 1MF 1CL 1SZ 1SS 1AZ 1AS 1BZ 1BS 1VZ 1VS 1MZ
1MS 2SP 2VO 2CC 2CA 2CB 2ST 2MF 2CL 2SZ 2SS
2AZ 2AS 2BZ 2BS 2VZ 2VS 2MZ 2MS ";
static const CHAR *
(* const readfns[sizeof(read_str)/4])(void) =
{
una, fnb, fnc, ...
};
cmdptr = strstr(read_str, buf);
if (cmdptr != NULL)
{
/*
* cmdptr points to the valid command, so compute offset,
* in order to get entry into function jump table
*/
offset = (cmdptr - read_str) / 4;
/* Call function and get pointer to reply*/
replyptr = (*readfns[offset])();
/* rest of the code goes here */
}
}
Сам код понятен. Есть массив мнемоник read_str. Если в функцю в качестве аргумента передали мнемонику и мы нашли ее в списке cmdptr = strstr(read_str, buf); то вызываем функцию привязанную к мнемонике replyptr = (*readfns[offset])(); . Вопрос почему тут const readfns[sizeof(read_str)/4])(void) деление на 4? убрать пробелы чтоб размер readfns соответсвовал размеру read_str? но деление на 4 не решает этого.
а...понял...мнемоника 3 чара + пробел итого 4. поделили на 4 получили количество мнемоник. иногда я туплю нипадецки.