(char)NULL , '\0' and 0 all mean the same thing in C's memset?

c, linux

Solution

The do not all mean the same thing, though they're likely to yield the same result.

`(char)NULL` converts the value of `NULL`, which is an implementation-defined null pointer constant, to `char`. The type of `NULL` may be `int`, or `void*`, or some other integer type. If it's of an integer type, the conversion is well defined and yields `0`. If it's `void*`, you're converting a null pointer value to `char`, which has an implementation-defined result (which is likely, but not guaranteed, to be 0).

The macro `NULL` is intended to refer to a null pointer value, not a null character, which is a very different thing.

Your macro `NULLC` is not particularly useful. If you want to refer to a null character, just use the literal constant `'\0'`. (And `NULLC` is IMHO too easily confused with `NULL`.)

The other two constants, `'\0'` and `0`, have exactly the same type (`int`) and value (zero).

(It's admittedly counterintutive that `'\0'` has type `int` rather than `char`. It's that way for historical reasons, and it rarely matters. In C++, character constants are of type `char`, but you asked about C.)

Problem

We are migrating a 32 bit application from rhel 5.3 to 6.4 We are getting an warning "Cast from pointer to integer of different size " on new system's `memset`. Do `(char)NULL`, `'\0'` and `0` all mean the same thing in C's memset? The following code is giving the warning in new environment. ``` #define NULLC (char)NULL #define MAX_LEN 11 … memset(process_name, NULLC, MAX_LEN + 1); strncpy(process_name, "oreo", MAX_LEN); ```

Original source