1

I 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?

#include <stdio.h>
int main()
{
    char a[10];
    char *b = a;

    if(b == NULL) 
      printf("b is NULL");
    else
      printf("b is not NULL");
      
    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Can you be more precise? What do you mean by "absolutely blank"? In your code, `b` will be the address of the array (that value may look peculiar, but it won't be zero); if you print any of the elements of `a`, they will have undefined values (because you haven't given them any values). – Adrian Mole Aug 09 '21 at 09:25
  • 1
    `b` contains the address of `a` (more precisely, the address of the first element of `a`). It will never be `NULL`, since `a` obviously has a non-NULL address. As for printing the values of `a`, you need to set them to something before you can meaningfully print them. Otherwise you're printing undefined values. – Tom Karzes Aug 09 '21 at 09:29
  • 1
    The real question is why did you think it would be NULL? – Lundin Aug 09 '21 at 10:02

3 Answers3

2
char a[10];
char *b = a;

Value stored in a Pointer to an undefined array is not NULL?

  1. The array a is very well defined. It is not initialized and the elements of the array have not determined values. So when you print the elements of the array a any values can be outputted. Read about undefined behaviour

  2. The pointer b is not "storing" any values. It stores the reference to the object having the char type. That object can "store" a char value, but not the pointer itself.

  3. As the array a is well defined and resides in the memory, the reference to its first element (as a decays to a pointer to the first element of this array) cannot be NULL.

I think you need to read the pointer chapter of your favourite C book.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • So, if i print the pointer b, like printf("%s", b); it should give me some junk instead of NULL right? – Karthik Gottipalli Aug 09 '21 at 09:41
  • Never NULL in this case. NULL is a special not existing reference. Using not initialized automatic variable is UB. You do not know what will happen. – 0___________ Aug 09 '21 at 09:42
  • @KarthikGottipalli You can print the pointer `b` using `printf("%p\n", (void *)b);`. That prints the actual pointer value rather than the junk string contents the pointer is pointing to. – Ian Abbott Aug 09 '21 at 09:52
  • @KarthikGottipalli You may be confused by the difference between null pointers and null-terminated strings. – Ian Abbott Aug 09 '21 at 09:55
1

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.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
0

The pointer b has a valid value. It points to the first elements of the array a.

char a[10];
char *b = a;

So it can not be equal to NULL.

On the other hand, the array a having automatic storage duration is not initialized. Its elements have indeterminate values.

If you will initialize the array with a string or a sequence of characters then they can be outputted using the pointer b.

For example

char a[10] = "Hello";
char *b = a;

puts( b );

or

char a[10] = "0123456789";
char *b = a;

printf( "%.*s\n", ( int )sizeof( a ), b );

If you want to output the value stored in the pointer b that is the address of the first element of the array a you may write for example

printf( "b = %p\n", ( void * )b );

The outputted value will be the same as in this call of printf

printf( "a = %p\n", ( void * )a );

You can write the following test program.

#include <stdio.h>

int main(void) 
{
    char a[10];
    char *b = a;
    
    if ( b == a )
    {
        puts( "b is equal to the address of the first element of a" );
    }
    
    return 0;
}

Its output will be

b is equal to the address of the first element of a
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335