Sujal Patel
Sujal Patel

Reputation: 81

what does ((double *)0) do internally in C

I want to understand what the compiler does when it encounters this statement and ((double*)0+1) statement.

Upvotes: 0

Views: 168

Answers (2)

vonbrand
vonbrand

Reputation: 11831

Nothing whatsoever... it is casting 0 (by definition, the NULL pointer) to a pointer to double. Presumably to say something like:

double *ptr;
....
ptr = ((double *) 0);
....
ptr = NULL;  /* Idiomatic */
ptr = 0;     /* Also allowed, for lazy fingers */

All three asignments above do exactly the same thing.

Unless the pointer value cast is 0, you have to be extra careful not to mess up. Most pointer casts are calling for undefined behaviour, they may "work" with today's compìler on your current machine and blow around your ears in a year's time.

Upvotes: 0

user529758
user529758

Reputation:

Since double * is a pointer type, it propagates the literal value 0 to the NULL pointer, then according to the rules of pointer arithmetic, it adds 1 to its value (therefore numerically, the result will be NULL + sizeof(double)).

Upvotes: 3

Related Questions