Q: Why does this code result in a core dump? I have this piece of C code below and whatever value I give to code[], I'm getting Segmentation fault (core dump). Do you have any ideas why this happens?
char code[] = "";
int main(int argc, char **argv)
{
  int (*func)();  
  func = (int (*)()) code;  //typecast the array code to be a function pointer
  (int)(*func)();   
}


A: Converting an object pointer to a function pointer is undefined behaviour. Simply put, a function pointer may only be converted to another function pointer, but not to an object pointer or vice versa.
Note that modern OSes protect data memory segments against execution of code by default ("no execute", NX), so you happily run into access violation exceptions.
Last not least: You very likely have to link/bind/relocate your code to do something useful.

A: Your shellcode is probably read-only, to make it executable you'd need:
 mprotect((void *)((uint64_t)code & ~4095), 4096, PROT_READ|PROT_EXEC);

