Value stored in a Pointer to an undefined array is not NULL?
The pointer value is the address of the array regardess of the fact that it (the array) has assigned values, so no, it will not be NULL.
always get the output of below program as "b is not NULL". When I try to print the values of a
or b
, they are absolutely blank. Any idea why this happens?
The specifier for printing a pointer value is %p
.
Live demo
char a[10];
char *b = a;
printf("\n%p", (void*)b);
printf("\n%p", (void*)a);
The output should be something like:
0x7ffec02d3d16
0x7ffec02d3d16
So, as you can see, the pointer value is not NULL but rather the address of the array.
If, on the other hand, you are printing the char array as if it was a string, i.e.:
printf("\n%s", b);
printf("\n%s", a);
Your program has undefined behavior, it may print nothing, it may print some random characters corresponding to the garbage data that is stored in the memory location that the declared array now occupies, these are the most common outcomes, but since there are no requirements for what a program exhibiting undefiend behavior should do, you can expect all manner of strange outcomes.