What would be a proper invalid value for a pointer?

前端 未结 7 672
抹茶落季
抹茶落季 2021-01-25 03:16

Suppose I have this code. Your basic \"if the caller doesn\'t provide a value, calculate value\" scenario.

void fun(const char* ptr = NULL)
{
   if (ptr==NULL) {         


        
7条回答
  •  渐次进展
    2021-01-25 03:33

    If you want a special value that corresponds to no useful argument, make one.

    header file:

    extern const char special_value;
    
    void fun(const char* ptr=&special_value);
    

    implementation:

    const char special_value;
    
    void fun(const char* ptr)
    {
        if (ptr == &special_value) ....
    }
    

提交回复
热议问题