Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to initialize a C pointer to NULL?

I had been writing things like

char *x=NULL; 

on the assumption that

 char *x=2; 

would create a char pointer to address 2.

But, in The GNU C Programming Tutorial it says that int *my_int_ptr = 2; stores the integer value 2 to whatever random address is in my_int_ptr when it is allocated.

This would seem to imply that my own char *x=NULL is assigning whatever the value of NULL cast to a char is to some random address in memory.

While

#include <stdlib.h> #include <stdio.h>  int main() {     char *x=NULL;      if (x==NULL)         printf("is NULL\n");      return EXIT_SUCCESS; } 

does, in fact, print

is NULL

when I compile and run it, I am concerned that I am relying on undefined behavior, or at least under-specified behavior, and that I should write

char *x; x=NULL; 

instead.

like image 652
fagricipni Avatar asked Apr 11 '17 06:04

fagricipni


1 Answers

Is it possible to initialize a C pointer to NULL?

TL;DR Yes, very much.


The actual claim made on the guide reads like

On the other hand, if you use just the single initial assignment, int *my_int_ptr = 2;, the program will try to fill the contents of the memory location pointed to by my_int_ptr with the value 2. Since my_int_ptr is filled with garbage, it can be any address. [...]

Well, they are wrong, you are right.

For the statement, (ignoring, for now, the fact that pointer to integer conversion is an implementation-defined behaviour)

int * my_int_ptr = 2; 

my_int_ptr is a variable (of type pointer to int), it has an address of its own (type: address of pointer to integer), you are storing a value of 2 into that address.

Now, my_int_ptr, being a pointer type, we can say, it points to the value of "type" at the memory location pointed by the value held in my_int_ptr. So, you are essentially assigning the value of the pointer variable, not the value of the memory location pointed to by the pointer.

So, for conclusion

 char *x=NULL; 

initializes the pointer variable x to NULL, not the value at the memory address pointed to by the pointer.

This is the same as

 char *x;  x = NULL;     

Expansion:

Now, being strictly conforming, a statement like

 int * my_int_ptr = 2; 

is illegal, as it involves constraint violation. To be clear,

  • my_int_ptr is a pointer variable, type int *
  • an integer constant, 2 has type int, by definition.

and they are not "compatible" types, so this initialization is invalid because it's violating the rules of simple assignment, mentioned in chapter §6.5.16.1/P1, described in Lundin's answer.

In case anyone's interested how initialization is linked to simple assignment constraints, quoting C11, chapter §6.7.9, P11

The initializer for a scalar shall be a single expression, optionally enclosed in braces. The initial value of the object is that of the expression (after conversion); the same type constraints and conversions as for simple assignment apply, taking the type of the scalar to be the unqualified version of its declared type.

like image 192
Sourav Ghosh Avatar answered Sep 24 '22 08:09

Sourav Ghosh