I have a question, well more of a verification.
In the program below, I'm packing a character array with some values. The first two are unsigned integers the next is a function pointer and the final is a string of characters...Now my question, is the casting correct for the function pointer? Just check the sections marked off with /*this section*/.
#include <stdio.h>
#include <string.h>
#define MAXLINE 4096
char *ca = "this is the string to pass";
void myhello(void)
{
fputs("Hello, World!\n", stdout);
}
int main(int argc, char**argv)
{
unsigned int i = 0;
char recvline[MAXLINE];
*(unsigned int*)&recvline[0] = 11111;
*(unsigned int*)&recvline[sizeof(unsigned int)] = 22222;
/*this section*/
*(void(**)(void))&recvline[2 * sizeof(unsigned int)] = (void(*)(void))&myhello;
/*this section*/
for (i = 0; i < strlen(ca); ++i)
{
recvline[2 * sizeof(unsigned int) + sizeof(void*) + i] = ca[i];
}
fputs("\n\ndisplay packed character array\n\n", stdout);
fprintf(stdout, "number one->%u\n", *(unsigned int*)&recvline[0]);
fprintf(stdout, "number two->%u\n", *(unsigned int*)&recvline[sizeof(unsigned int)]);
fputs("call function myhello->", stdout);
/*this section*/
(*(void(**)(void))&recvline[2 * sizeof(unsigned int)])();
/*this section*/
fprintf(stdout, "string->%s\n", &recvline[2 * sizeof(unsigned int) + sizeof(void*)]);
return 0;
}