Is NULL always false?

前端 未结 13 1619
孤城傲影
孤城傲影 2020-11-27 04:44

Is it safe to assume that NULL always translates to false in C?

void *somePtr = NULL;

if (!somePtr) {
  /* This will always be executed? */
}
<         


        
13条回答
  •  萌比男神i
    2020-11-27 05:35

    Yes (at least for any standards compliant C compiler!)

    From the comp.lang.c FAQ:

    Q: Is the abbreviated pointer comparison ``if(p)'' to test for non-null pointers valid? What if the internal representation for null pointers is nonzero?

    A: It is always valid.

提交回复
热议问题