2

In a lot of c code snippets i see (void *)0,what does casting an integer with a pointer mean?

melpomene
  • 84,125
  • 8
  • 85
  • 148
Henok Tesfaye
  • 8,287
  • 13
  • 47
  • 84
  • 4
    `(void *)0` is a way of writing a null pointer constant; so is `NULL` and in many contexts `0` will also be converted to a null pointer constant if needed (and there are other ways of writing such pointers). If the value being cast is not zero, then it has a different purpose, and is generally less portable (meaning, not at all portable). – Jonathan Leffler Apr 30 '18 at 20:50
  • 3
    The "duplicate" doesn't really cover `(void *)0` – M.M Apr 30 '18 at 20:51
  • 2
    I agree with @M.M — the duplicate [What does it mean to convert `int` to `void *` or vice versa?](https://stackoverflow.com/questions/8618637/what-does-it-mean-to-convert-int-to-void-or-vice-versa)) doesn't mention `(void *)0` which does have a portable and sensible meaning. The duplicate was discussing `(void *)5` and similar casts, primarily in the context of the 'data' argument to `pthread_create()`, which is a rather specialized (ab)use of casting an integer to a void pointer.\ – Jonathan Leffler Apr 30 '18 at 20:52
  • 1
    Thank you @JonathanLeffler, so it is another way of writing the keyword NULL. – Henok Tesfaye Apr 30 '18 at 21:10
  • 2
    @HenokTesfaye: Writing `(void *)0` is another way of writing `NULL`, but `NULL` is not a keyword — it is just a macro defined when you include any of half-a-dozen standard C headers (``, ``, ``, ``, ``, ``). – Jonathan Leffler Apr 30 '18 at 22:18

1 Answers1

2

In general, casting an integer to a pointer "means" creating a pointer value pointing to that exact numeric address in memory. But I have put the word "means" in quotes here, because the actual meaning is more complicated.

There are basically three subcases.

  1. If the integer is the constant 0 (and especially if the casted-to pointer type is void *), this is a null pointer constant. By definition, it is the syntactic construct that gets you a null pointer value. By definition, a null pointer points nowhere (it "compares unequal to the pointer to any actual object").
  2. In old-school C programming, and to this day in embedded C programming, if the integer is nonzero, it's an actual memory address you're trying to access.
  3. In modern, portable, high-level C programming, and especially here on Stack Overflow, the meaning is implementation-defined at best or undefined at worst, meaning that you shouldn't do it and you probably shouldn't even ask about it, since no one can say for sure what it means.

In answer to your comment in response to Jonathan Leffler's, NULL is not actually a keyword, but rather a preprocessor macro which expands to either (void *)0 or, maybe, plain 0. (How it is that it's possible for a plain 0, without a cast, to work properly as a null pointer constant is a fascinating but frequently badly misunderstood side question which I am not going to go into here. See section 5 in the C FAQ list if curious.)

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
  • A small quibble: It can expand to `((void*)0)` but not to `(void*)0`. See [N1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) 7.1.2p5, which requires definitions of object-like macros to be protected by parentheses when necessary. – Keith Thompson Apr 30 '18 at 21:58