What is an Uninitialized or Illegal Pointer in C?

Question: What is an Uninitialized or Illegal Pointer in C Language and What Happens on Indirection of an Illegal Pointer?

Answer: As we know, pointers, whether initialized or uninitialized, hold addresses. And we perform Indirection on pointers to access values, indirectly, at locations pointed to by them. For example:

/* access_initialized_pointer.c -- Program accessing initialized pointers */
#include <stdio.h>
 
int main(void)
{
    int some = 20;
    int *iptr = &some;  /* iptr initialized with address of 'some' */
 
    printf("\nAddress of integer \'some\' in exp. \'int some = 20\' is %p\n"
           "and where \'iptr\' is pointing to, in exp. 'int *iptr = &some',"
            " is\n%p\n", &some, iptr);
 
    printf("\nLet's see what happens when we access initialized pointer "
           "\'ptr\' in exp.\n\'int *iptr = &some\'\n");
    printf("This gives Value %d of variable \'some\'\n\n", *iptr);
 
    return 0;
}

Notice output of above program, when run on Linux machine,

 
Address of integer 'some' in exp. 'int some = 20' is 0x7fff34ff3454
and where 'iptr' is pointing to, in exp. 'int *iptr = &some', is
0x7fff34ff3454
 
Let's see what happens when we access initialized pointer 'ptr' in exp.
'int *iptr = &some'
This gives Value 20 of variable 'some'

Observe here that addresses of integer some and location where iptr is pointing to are same. And we performed Indirection on iptr to access the value at location pointed to by iptr, precisely the storage allocated to the integer some, to get the value of some which is 20.

advertisement
advertisement

Now, this time we turn to see an example with Uninitialized pointer, then performing Indirection on it and observe the output.

/* 
 * deref_uninitialized_ptr.c -- Program shows what happens when accessing
 * uninitialized pointers
 */
#include <stdio.h>
 
int main(void)
{
    int *ip1;   /* ip1 declared pointer-to-integer, but not initialized */
    int *ip2;   /* ip2 declared pointer-to-integer, but not initialized */
    char *cp;   /* cp declared pointer-to-character, but not initialized */
    float *fp;  /* fp declared pointer-to-float, but not initialized */
 
    printf("Default Address \'ip1\' holds as %p\n", ip1);
    printf("Default Address \'ip2\' holds as %p\n", ip2);
    printf("Default Address \'cp\' holds as %p\n", cp);
    printf("Default Address \'fp\' holds as %p\n", fp);
 
    printf("\nNow, see, what happens when we access uninitialized pointers"
           "...\n");
    printf("%d %d %c %f\n", *ip1, *ip2, *cp, *fp);
 
    return 0;
}

Output:

Note: Join free Sanfoundry classes at Telegram or Youtube
Default Address 'ip1' holds as (nil)
Default Address 'ip2' holds as 0x7fff69f3bd80
Default Address 'cp' holds as 0x400420
Default Address 'fp' holds as (nil)
 
Now, see, what happens when we access uninitialized pointers...
Segmentation fault (core dumped)

So, what happened here, when we attempted to perform Indirection on uninitialized pointers? This caused an ERROR which ABORTED the program & displayed the massage “Segmentation fault”. What does this mean? Let’s try to know. Actually, when we compiled the program, compiler allocated the program a fixed storage in memory. This is called ‘Address Space’ of the program. But Uninitialized pointer, ip1 holds the default address, here nil fortunately, pointing nowhere in the memory, and when performed Indirection upon ip1, tried to access the location, in memory allocated to program, but not found and resulted in error causing Segmentation fault and aborted the program.

Generally, Uninitialized pointers hold default addresses which refer to locations beyond the scope of memory allocated to program by compiler. And hence, upon Indirection of such pointers Segmentation Fault occurs!

Sanfoundry Global Education & Learning Series – 1000 C Tutorials.

advertisement
If you wish to look at all C Tutorials, go to C Tutorials.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.