How can i write an if statement which tells to the program if the pointer is assigned or not?
WRONG example
if (*badpdr[0]==0);
??
How can i write an if statement which tells to the program if the pointer is assigned or not?
WRONG example
if (*badpdr[0]==0);
??
You mean if (badptr==NULL)
?
Note that you have to initially set your pointer to NULL when you define it or when you "unassign it" (e.g., you delete the object it refers to). Otherwise, it will contain junk and this test would fail because the value would not be a 0.
You can also do the comparison to 0 instead of NULL, there's already enough arguments on SO which is the correct form so I won't repeat them.
char *ptr=NULL;
initalize the pointer to NULL; then later on you can check if it is NULL to see if it is valid before you try to deference it.
You can install signal handler for SIGSEGV
and then try to dereference the pointer - if you find yourself inside signal handler, pointer wasn't assigned (or points to invalid memory address, which basically is the same thing)
However there could be the situations when unassigned pointer points to valid address that belongs to process memory space - in this case there will be no SIGSEGV
raised.