How do we check if a pointer is NULL pointer?

前端 未结 8 2248
一向
一向 2020-12-01 11:56

I always think simply if(p != NULL){..} will do the job. But after reading this Stack Overflow question, it seems not.

So what\'s the canonical way to c

8条回答
  •  甜味超标
    2020-12-01 12:13

    The representation of pointers is irrelevant to comparing them, since all comparisons in C take place as values not representations. The only way to compare the representation would be something hideous like:

    static const char ptr_rep[sizeof ptr] = { 0 };
    if (!memcmp(&ptr, ptr_rep, sizeof ptr)) ...
    

提交回复
热议问题