View Single Post
Ambitious contributor
29Jan2011,19:05  
poornaMoksha's Avatar
Shishir pointed out in the very first post of this discussion that " Its not necessary to initialize the pointer at the time of declaration". I think its not mandatory but still we should always make sure that a pointer is initialized to NULL whenever it is declared because :

Code:
int main()
{
    char *ptr;
    int retval = 0;
    retval = someFunc();
    if(retval)
        ptr = "I got some address";
    someOtherFunc(ptr);
}

void someOtherFunc(char *ptr)
{
    if(ptr)
    {
        // use ptr
     }
}
Now in the above code snippet, suppose the value of 'retval' is 0 which ensures 'ptr' does not get address of string "I got some address" and inside the function 'someOtherFunc' the condition 'if(ptr)' may well be true as ptr may be holding some garbage address which is non-null and hence when we use ptr we may well observe a crash as through 'ptr' the program would try to access the garbage address