-2

Are the below statements same?

char *PTR='\0';

&

char *PTR=NULL;
mch
  • 9,424
  • 2
  • 28
  • 42

3 Answers3

3

Accidentally, yes.

'\0' is a character with ASCII code 0; as such, it is an integer with value 0. NULL is usually also an integer with value 0. They are typically of different sizes, but the compiler will recognise that in this case, so in both cases PTR ends up as a null-pointer (i.e. pointer with value 0).

If you had written char *PTR="\0"; with double-quotes, that's a very different situation: "\0" would be allocated as a two-character array, with both characters being ASCII 0. PTR would be initialised to point at the first of the two zeroes.

Amadan
  • 191,408
  • 23
  • 240
  • 301
  • 1
    (a) `'\0'` is a character constant with value zero. Whether it is ASCII or not is implementation-dependent. (b) `NULL` is not “usually” an integer with value zero. Per the C standard, it may be either an integer constant expression with value zero or such an expression cast to `void *`. (c) Reasoning about the expressions in terms of being different sizes is a mistake. The semantics of C are defined in terms of types, and there are issues of type compatibility and rules even if sizes are the same. – Eric Postpischil Jul 18 '19 at 12:08
  • @EricPostpischil: a) I said ASCII 0 to differentiate it from ASCII 48, as saying "zero" is confusing. b) yes, it should be. c) you are correct. – Amadan Jul 18 '19 at 12:10
1

No they are not, although they will produce the same result. '\0' is a single byte character which we use as a null terminating character on the string. NULL is (void*)0, which is a pointer, and is a 64bit value (or 32bit depending on architecture). You'd be better off using NULL (or nullptr) for pointer values, and keep '\0' as a way to terminate a string. Having said that though, converting '\0' to a pointer should be fine. Going the other way may produce a warning (converting 64bit value to an 8bit one).

char *PTR = '\0';
char CHAR = '\0';

char *PTR = NULL;
char CHAR = NULL;  ///< will generate a warning
robthebloke
  • 9,331
  • 9
  • 12
  • Re "*NULL is (void*)0*", According to [this](https://stackoverflow.com/a/2599766/589924), `NULL` can be either `0` or `(void*)0`. – ikegami Jul 18 '19 at 05:03
  • 1
    In C, `\0` is an `int`, the same as any other character literal. So it is just another way to write `0`. – rici Jul 18 '19 at 05:51
  • `NULL`. or a pointer in general, is not restricted to being a 64-bit or 32-bit value. It is better to convey type semantics to a student than to distinguish values based on widths. The conversion from a pointer to a `char` would be a problem due to the rules about converting types, not due to any rule about converting a 64-bit value to an 8-bit value. `nullptr` is C++, not C. – Eric Postpischil Jul 18 '19 at 12:19
-1

NULL is a macro. So it depends on what its definition is. Usually NULL is just 0.

Here is already a good answer

CatPlusPlus
  • 118
  • 8
  • According to [this](https://stackoverflow.com/a/2599766/589924), `NULL` must be either `0` or `(void*)0`. – ikegami Jul 18 '19 at 05:01
  • @ikegami: `#define NULL ((void*)42)` will throw a warning, but will work (until something bad happens). – Amadan Jul 18 '19 at 05:03
  • @Amadan, Yes, it would be illegal for the compiler to provide that. Did you not visit the link I provided? – ikegami Jul 18 '19 at 05:04
  • @ikegami I did not mean for the compiler to provide that. A silly programmer can. The _compiler's_ `NULL` will always be 0, but it does not mean any `NULL` you encounter is guaranteed to be (though it's either malice, obfuscated C code contest or extreme idiocy that would cause it to happen). – Amadan Jul 18 '19 at 05:05
  • @ikegami yes, thanks for that clarification – CatPlusPlus Jul 18 '19 at 05:06
  • @Amadan, They could also redefine `char`. That's all quite irrelevant. – ikegami Jul 18 '19 at 05:10