-1

I understand that the definition of a string in C is that the string has to have NUL character at the end. I am also aware that the following statements creates NUL character of a string:

 str[4] = 0;
 str[4] = '\0';

However, when you use this statement

 str[4] = NULL;

I am getting this message:

string1.c:15:12: warning: assignment makes integer from pointer 
without a cast [enabled by default]

My understanding of NULL is that it is a macro with the definition of ((void *)0). Now why is this assignment causing this warning message? What this warning message really mean? Can someone translate this message so that it is easier to understand?

Nguai al
  • 958
  • 5
  • 15
  • 4
    NUL is a character, which, in C, is a type of integer. NULL is a pointer. Does that help? – zwol Apr 29 '17 at 01:20
  • 2
    See [this question](http://stackoverflow.com/questions/1296843/what-is-the-difference-between-null-0-and-0) – J...S Apr 29 '17 at 01:22
  • @zwol - Is there a way to use NULL to a string without getting this warning message? – Nguai al Apr 29 '17 at 01:28
  • 1
    @Nguai al - Why would you want to assign NULL to a character? NULL is for pointers. If you want the `\0` char, Just use it directly instead of using the macro NULL. – PointerToConstantChar Apr 29 '17 at 01:30
  • @Nguaial Sure there is. But here, you are trying to assign `NULL` to `str[4]` which is a *character*. You could do: `const char *j = "hello"; j = NULL;`. Here `j` is a string that you then assign `NULL` to. – David Schwartz Apr 29 '17 at 01:34
  • @Nguaial Yes, to assign NUL to an element of a string, write `str[n] = '\0'`. – zwol Apr 30 '17 at 16:55

1 Answers1

2

NULL and the NUL \0 are very different things. NULL is an implementation-defined macro that is defined to be either 0, (char*)0, or (void*)0, And if your implementation uses either (void*)0 or (char*)0 then to assign it to a char it would be implicitly converted to an integer(chars are just 1-byte-integers). The NUL \0 character on the other hand, Is a byte-size 0.

  • 1
    However `NULL` is defined for a specific implementation, it is a macro with a _null **pointer** constant_ and should not be assigned to an integer. A null pointer value is not guaranteed to have a representation or conversion value of _all bits zero_. "NUL" otoh is not a format part of the statndard, it is just trhe ASCII name of a character code with all bits zero. In C it is not "an invisible character" (that depends on the character set and glyph mapped to this position. In fact it makes an array a string in C. C does not have a string type. – too honest for this site Apr 29 '17 at 01:38
  • NUL is a byte size zero. It occupies a byte size zero. – Nguai al Apr 29 '17 at 02:12
  • @Nguaial: Please provide a reference to the standard where it even mentions a symbol/macro/etc `NUL`. And it does not mean "noting, but is an ASCII code like "CR", "LF", etc. In C a _null character_ has a very special meaning, which is definitively not "nothing"! – too honest for this site Apr 29 '17 at 02:15
  • @nguai - i still don't understand what you are trying to communicate. – EvilTeach Apr 29 '17 at 02:32
  • @EvilTeach - Which part you are not clear? – Nguai al Apr 29 '17 at 02:38
  • Nul is a byte size zero. – EvilTeach Apr 29 '17 at 03:50