In C, can an address of a pointer be 0? -
i'm reading solution problem in book cracking coding interview (q 1.2). objective there implement function void revers(char* str)
in c reverses null terminating string.
the solution code looks this:
void reverse(char *str) { char* end=str; char tmp; if(str) { while(*end) { end++; } end--; //code reverse } }
here, str
contains address right? , if(str)
evaluate false
if str
0
, right?
so i'm saying is, there no chance str
contain address 0x0000
, evaluating if(str)
false
?
str
indeed contain address (it's pointer char
), , if(str)
evaluate false iff str
equal null pointer.
note null pointer not required standard refer address 0
; however, standard mandates literal value of 0
when used in pointer context must interpreted compiler address of null pointer -- whatever might be.
this means test if(p == 0)
guaranteed same if(p == null)
. also, conditional if(p)
guaranteed same if(p != 0)
.
conclusion: code detect null pointer, but that's not technically same pointer points address 0 (even though in practice going find is).
Comments
Post a Comment