Reputation: 453
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);
Upvotes: 2
Views: 6147
Reputation: 3460
0 = zero of int datatype
'\0' = (char)0 //null char
NULL = (void*)0 //null pointer
See how they are interlinked to each other. Gcc often gives warnings for all the typecast that are implicitly done by the compiler.
You are using
#define NULLC (char)NULL
.....
memset(process_name, NULLC, MAX_LEN + 1);
equivalent to:
memset(process_name, (char)NULL, MAX_LEN + 1);
equivalent to:
memset(process_name, '\0', MAX_LEN + 1);
You are passing char data (ie; '\0'
) as second parameter where "unsigned int" data is accepted. So compiler is converting it to unsigned int implicilty and thus giving typecast warning. You can simply ignore it or change it as:
memset(process_name, 0, MAX_LEN + 1);
Upvotes: 0
Reputation: 263497
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.)
Upvotes: 11
Reputation: 20878
In defining NULLC, you are casting NULL
from a native pointer (64-bits, probably defined as (void*)0
) to char
(8-bits). If you wanted to declare NULLC, you should just do
#define NULLC 0
and do away with NULL
and the (char)
. The formal argument to memset
is int
, not char
.
Upvotes: 0
Reputation: 13171
0
and '\0'
are both the integer 0 (the type of a character literal is int
, not char
), so they are exactly equivalent. The second argument to memset is an int, from which only the low-order 8 bits will be used.
NULL is a different beast. It is a pointer type that is guaranteed by the standard to be different from any pointer to a real object. The standard does NOT say that this is done by giving it the value 0, though it must compare equal to zero. Also, it may be of different width than int
, so passing it as the second argument to memset()
might not compile.
Upvotes: 3
Reputation: 3048
They all have the same value 0
but they don't mean the same thing.
(char)NULL - You are casting the value of NULL pointer to character with value 0
'\0' - End of string character with value 0 (NUL)
0 - 32 bit integer with value 0.
You are getting a warning because somewhere in your code you're likely using something like:
short somevar = NULL;
or something similar.
Upvotes: 6