How to Migrate from HP-UX to Linux: NULL
On Linux I tried to compile a C program that I had brought over from HP-UX and got the following warning:
foo.c: In function 'bar':
foo.c:42: warning: assignment makes integer from pointer without a cast
The code in question terminates a character string with a nul character. However, instead of coding the nul character as some variety of zero, it codes it as NULL. This is bad form, because NULL is intended to be used as a pointer value, not as a number or character. The compiler may or may not issue a warning, depending on the vendor’s taste, and depending on how the NULL macro is defined.
NULL may be defined in either of two ways:
#define NULL 0
…or:
#define NULL (void *) 0
In the former case, NULL is of type int, and may be assigned to a character variable without a qualm (apart from stylistic objections). In the latter case, NULL is of type void *, and the compiler may be a little queasy about assigning it to a character.
On Linux, gcc defines NULL as a pointer, and issues the warning. I’m not sure what the HP-UX compiler does, because the #define is tangled in a thicket of #ifdefs. It may define NULL as an int, which would probably not result in a warning. Or maybe we did get a warning and ignored it.
The offending line is embarrassingly gauche, but not actually broken. Either compiler will almost certainly do the right thing.
I can fix the code before or after migration. I could even leave it unchanged, if I were willing to ignore the compiler warning. However I don’t want to get in the habit of ignoring compiler warnings, especially when I can easily eliminate them.
This assessment applies to any case where you misuse NULL as a numeric value. If you see the same compiler warning in a context where the pointer value is anything other than NULL, the issue is more serious and should be addressed immediately. However those cases probably would have already been noticed because they would introduce nasty bugs.
Learn more about Legacy Systems Support, contact Unified Development, Inc.