When you declare a pointer like int* p;
. Is p
initially NULL
?
Asked
Active
Viewed 92 times
1 Answers
4
Depends whether it's declared defined global (or static
) or not. For the former case "yes", for the latter "no".
To turn it around: Non global, non static
variables are not implicitly initialised in C.

alk
- 69,737
- 10
- 105
- 255
-
But some compilers might initialize non-`static` local pointers to `NULL`, right? – Spikatrix Mar 06 '16 at 11:33
-
1Compilers *might* support any kind of *extensions* to the C standard, sure. @CoolGuy However, using those extensions, relying on them might render the code unportable. – alk Mar 06 '16 at 12:04
-
2@CoolGuy: do not rely on that, such compilers are extremely rare and your code would invoke undefined behavior if compiled with another compiler. Conversely, **do** turn on extra warnings to allow the compiler to tell you about pointers used before they have been initialized. The compiler cannot always tell, and sometimes is too conservative, but in most cases, these warnings are life savers. – chqrlie Mar 06 '16 at 12:08
-
Hmm, Detail: _declaring_ a global pointer like `extern void *ptr` does not make it equivalent to `NULL`. _defining_ a global pointer like `void *ptr` does. – chux - Reinstate Monica Mar 06 '16 at 20:37
-
1My wording was inaccurate, yes. Thanks. Fixed. @chux – alk Mar 08 '16 at 08:09