Here's the whole code with the relevant code in red
Code:
#include <stdio.h>
#include <stdlib.h>
void printer()
{
printf("in printer\n");
}
int main()
{
int test;
void *test_int = malloc(8);
void *test_float = malloc(8);
void *test_funcptr = malloc(8);
*(int*)test_int = 0;
*(int*)((int*)test_int+1) = 1;
*(int*)test_float = 1;
*(float*)((int*)test_float+1) = 2;
*(int*)test_funcptr = 2;
*(int*)((int*)test_funcptr+1) = (int)&printer;
test = (int)test_funcptr;
printf("trying to call printer from test_funcptr\n");
(*((int*)test+1))();
printf("assigning test_int to test\n\n");
test = (int)test_int;
printf("the value of test = %i, the type of test = %i\n", *((int*)test+1), *(int*)test);
printf("adding 762 to test\n");
*((int*)test+1) += 762;
printf("the value of test = %i, the type of test = %i\n", *((int*)test+1), *((int*)test));
printf("\nassigning test_float to test\n\n");
test = (int)test_float;
printf("the value of test = %f, the type of test = %i\n", *(float*)((int*)test+1), *((int*)test));
printf("adding 1.234 to test\n");
*(float*)((int*)test+1) += 1.234;
printf("the value of test = %f, the type of test = %i\n\n", *(float*)((int*)test+1), *((int*)test));
free(test_int);
free(test_float);
free(test_funcptr);
return 0;
}
test holds the memory address of the function pointer as an int. I want to call that function using only test and casting it to the function pointer type.