diff --git a/casting_pointer/compile.sh b/casting_pointer/compile.sh new file mode 100755 index 0000000000000000000000000000000000000000..d246e954b50323926b1db0114cd90fdb594d17af --- /dev/null +++ b/casting_pointer/compile.sh @@ -0,0 +1,3 @@ +# Compile a shared library +#gcc -shared -o libmean.so.1 -fPIC mean.c +gcc -o main -fPIC main.c diff --git a/casting_pointer/main b/casting_pointer/main new file mode 100755 index 0000000000000000000000000000000000000000..10c9409e09421519a3288a986fdb6b63949d7adb Binary files /dev/null and b/casting_pointer/main differ diff --git a/casting_pointer/main.c b/casting_pointer/main.c new file mode 100644 index 0000000000000000000000000000000000000000..a93ed2609778144c8965549008eee9441fa860d5 --- /dev/null +++ b/casting_pointer/main.c @@ -0,0 +1,41 @@ +#include <stdio.h> +#include <stddef.h> + +typedef int (*fn_def)(void); + +int test1() +{ + // function to call + printf("Test\n"); +} + +void convert_and_call(void *fnvptr_val) +{ + // convert into pointer + fn_def fnptr = (fn_def)fnvptr_val; + fnptr(); +} + + +int main() +{ + void* fnvptr = (void*)&test1; + + + // print adress + printf("%p\n", &test1); + + // cast into char array + char* a=(char*)fnvptr; + + // print stuff + printf("%s\n", a); + printf("size: %ld\n",sizeof(a)); + + // cast into new void thing + void * fnvptr2 = (void*)a; + printf("%p\n", fnvptr2); + + // // convert into pointer + convert_and_call(fnvptr2); +}