4

I have found in some code NULL pointer is defined as follows -

#define NULL ((char *)0)

I found these code compiles fine. But I did not understand how this works. Can anyone please explain how 0 is casted to a char pointer?

And is it valid to use it as a FILE pointer making null -

FILE *fp = NULL;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
ShafiH
  • 85
  • 1
  • 8
  • 4
    It works...poorly (in fact, I don't think it's allowed). You want `((void *)0)` instead. As to how it works: the standard defines a conversion from an integer constant with the value 0 to a pointer, and specifies that the result will always be a null pointer. – Jerry Coffin Mar 31 '15 at 22:30
  • @Jerry Coffin, yes you are right. While compiling the code I found the code compiles without any error but with the following warning - `warning: "NULL" redefined [enabled by default] /usr/lib/gcc/i686-linux-gnu/4.6/include/stddef.h:402:0: note: this is the location of the previous definition` But I did not understand how this works. – ShafiH Mar 31 '15 at 22:33
  • 1
    I think this question is already answered here: http://stackoverflow.com/questions/1296843/what-is-the-difference-between-null-0-and-0 – Mohit Mar 31 '15 at 22:35
  • Once upon a long time ago, that was the normal way to write NULL; we are talking about in the 1980s here, before there was a standard and before `void *` was uniformly supported. When the C standard was published (1989. 1990), it became less appropriate; it should be a `((void *)0)` instead. Maybe your code is old enough that it is using the pre-standard definition. – Jonathan Leffler Mar 31 '15 at 22:47
  • `((char *)0)` is not a valid null pointer constant (in C since the 1989 ANSI standard) and therefore not a valid definition for `NULL`. Given that definition, `FILE *fp = NULL;` is a *constraint violation*, requiring a compile-time diagnostic (and perhaps rejecting the program). – Keith Thompson Mar 31 '15 at 22:51
  • @KeithThompson: Do you have a citation for that? *Any* type can have an appropriately typed nullpointer, and the fact that you may round-trip pointers of one type through pointers of another suggests that you could well use `(char*)0` as a general nullpointer, with the caveat that you have to do explicit casts. – EOF Mar 31 '15 at 23:46
  • @EOF: Certainly evaluating the expression `((char *)0)` yields a null pointer at runtime. But it is not a *null pointer constant*, which is defined as a constant integer expression with the value 0, or such an expression cast to `void*`. It's a plausible definition of `NULL` for a pre-ANSI implementation, which may or may not have required a cast when assigning to another pointer type. – Keith Thompson Apr 01 '15 at 00:02
  • Any integer can easily be casted to any kind of pointer since these no real difference between a pointer and an integer: just consider the pointer as a memory address, which IS an integer. – Joël Hecht Apr 01 '15 at 05:29
  • @JoëlHecht And whiat do you do about the implementations which don't use all-0s for the internal null pointer representation? In fact, there can be huge differences between pointers and integers. Just think about the old MS-DOS age/Real Mode pointer model... – glglgl Apr 01 '15 at 08:12

1 Answers1

1

The C library Macro NULL is the value of a null pointer constant.It may be defined as ((void*)0), 0 or 0L depending on the compiler vendor. Depending on compiler,declaration of NULL can be

#define NULL ((char *)0)

or

#define NULL 0L

or

#define NULL 0

And is it valid to use it as a FILE pointer making null--> Yes.

user4291320
  • 302
  • 1
  • 12