Casting a void * to a double segmentation fault

Advertisements

I found the following answer: Convert void* to double

However, I am getting segmetation fault. Why is that?

Code:

void * p = (void *) 1234;
double n = *(double *)p;

>Solution :

Pointer p is assigned to address 1234 that you chose at random?. You are then referencing a random address. It does not correspond to a valid allocated chunk of memory.

Leave a ReplyCancel reply