什么是指针的合适的无效值? [英] What would be a proper invalid value for a pointer?

查看:141
本文介绍了什么是指针的合适的无效值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有这个代码。

void fun(const char* ptr = NULL)
{
   if (ptr==NULL) {
      // calculate what ptr value should be
   }
   // now handle ptr normally
}

并使用

fun();          // don't know the value yet, let fun work it out

fun(something); // use this value

但是,事实证明,ptr可以有各种类型的值,包括NULL,所以我不能使用NULL作为调用者不提供ptr的信号。

However, as it turns out, ptr can have all kinds of values, including NULL, so I can't use NULL as a signal that the caller doesn't provide ptr.

所以我不知道什么默认值给ptr现在改为的NULL。我可以使用什么魔法值?有人有想法吗?

So I'm not sure what default value to give ptr now instead of NULL. What magic value can I use? Does anybody have ideas?

推荐答案

void fun()
{
   // calculate what ptr value should be
   const char* ptr = /*...*/;

   // now handle ptr normally
   fun(ptr);
}

这篇关于什么是指针的合适的无效值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆