0

I'm looking for the official documentation or documentation alternative to the official one which explains the (nil) locution in C, but it seems very difficult to find it. Could you suggest me where to find it please?

I got (nil) address when I tried to print, with printf and %p, the address of a not initialized int pointer.

#include <stdlib.h>
#include <stdio.h>

int main()
{
int *pi;

    printf("Indirizzo pi: %p\n", pi);
    return 0;
}

EDIT: my goal is to get more informations about the word nil; I know that my example is wrong because I didn't initialized the pointer, I used it only to show you where I found nil.

Gennaro Arguzzi
  • 769
  • 1
  • 14
  • 27
  • 2
    can you provide code? – L.Grozinger Jul 10 '20 at 10:52
  • @L.Grozinger see the edit please. – Gennaro Arguzzi Jul 10 '20 at 10:53
  • 4
    `pi` is an uninitialized variable. It's strange that its value is `NULL`, but the compiler likely noticed that you are invoking undefined behavior and just emitted a NULL pointer value while calling `printf` (you can see [here](https://godbolt.org/z/ssxE1G) that both functions generate the same code). Howerver, if you tried to pass this pointer it to a function and then dereferenced it there, it would have likely caused an access violation. – vgru Jul 10 '20 at 10:53
  • 3
    The sequence of characters printed for the value of `void *` type corresponding to the `%p` printf format specifier is produced in an implementation-defined manner. – Ian Abbott Jul 10 '20 at 10:54
  • it's normally called NULL – user253751 Jul 10 '20 at 10:54
  • Does this answer your question? [Difference between uninitialized and null pointer](https://stackoverflow.com/questions/32211593/difference-between-uninitialized-and-null-pointer) – underscore_d Jul 10 '20 at 10:55
  • Hello @underscore_d thank you for the link. I prefer to see official documentation not only to learn more about (nil), but also because in the future I'll know where to search other official stuff related to C. – Gennaro Arguzzi Jul 10 '20 at 10:57
  • 1
    What does it print if you set `pi = NULL;` explicitly? Also, the argument corresponding to the `%p` specifier must be compatible with `void *`, so you should cast it: `printf("Indirizzo pi: %p\n", (void *)pi);`. – Ian Abbott Jul 10 '20 at 10:57
  • 3
    @GennaroArguzzi If by official documentation you mean the C standard, there is no requirement in there to print `(nil)` for any pointer value. It is implementation-defined. – Ian Abbott Jul 10 '20 at 10:59
  • 3
    @GennaroArguzzi The Standard doesn't specify the format of `printf("%p", some_void_ptr)`, and it does say you can't do anything with an uninitialised variable except give a value to it. That's official, and that's all there is to it. – underscore_d Jul 10 '20 at 11:00
  • 4
    Official documentation? Please see [Where do I find the current C or C++ standard documents?](https://stackoverflow.com/a/83763/) – Weather Vane Jul 10 '20 at 11:01
  • Hello @WeatherVane could you tell me the exact document and page or an alternative way to find informations about nil please? I have the ISO you suggested me, but there isn't any information about nil (maybe because I'm looking wrong). – Gennaro Arguzzi Jul 10 '20 at 15:36
  • Hi @underscore_d I need only to find informations about nil...I know that my example is wrong (I reported it only to tell you where I discovered this strange word: "nil"). – Gennaro Arguzzi Jul 10 '20 at 15:39
  • 2
    Refer to the document that relates to the language and compiler version you are using. Free draft versions are easy to find. The word **nil** does not appear in the C18 document I have. BTW MSVC politely outputs `(null)` when you pass `printf()`a null pointer value but `puts()` does not. It is not a language requirement, just a courtesy. – Weather Vane Jul 10 '20 at 15:40
  • @WeatherVane my gcc version is: gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0...In the following link I read that: gcc does not conform to any of the ANSI/ISO C standards (source: https://stackoverflow.com/questions/14737104/what-is-the-default-c-std-standard-version-for-the-current-gcc-especially-on-u) – Gennaro Arguzzi Jul 10 '20 at 15:42
  • 1
    You'll have to look it up yourself, sorry. I would quote it to back up an assertion I made, if asked. I think you'll find that is MS Visual C that is the rogue. VC and GCC both have non-standard language extensions. – Weather Vane Jul 10 '20 at 15:43
  • 3
    @GennaroArguzzi **"by default"**, it allows extensions that are not strictly confirming. There are switches to make it Standard-compliant. But if the Standard doesn't say what `printf("%p")` should do, other than display a the value of pointer _in some way_, then implementations can do whatever they like, and frankly it's not that interesting why they have such slight differences, and you'd probably have to ask some coder from 1992. – underscore_d Jul 10 '20 at 15:49
  • thank you @underscore_d – Gennaro Arguzzi Jul 10 '20 at 15:53

0 Answers0