Cpp_Primer_Practice icon indicating copy to clipboard operation
Cpp_Primer_Practice copied to clipboard

Exercise2.23 there might be a mistake here

Open Zhangxh251 opened this issue 2 years ago • 1 comments

原题:给定指针 p,你能知道它是否指向了一个合法的对象吗?如果能,叙述判断的思路;如果不能,也请说明原因。

我的看法: 把 if(p) 放在 try 中 , 当程序块顺利执行时,表示p指向了合法的对象;当程序块出错跳转到catch语句时,表示p没有指向合法的对象。

答案思路来源- 牛客网 https://m.nowcoder.com/questionTerminal?uuid=3c218c5306324eaf9de45a74932f0b9e

Zhangxh251 avatar Apr 17 '22 13:04 Zhangxh251

这个解法应该是有前提:指针初始化为NULL/0,否则if(p)其实等价于 if (p!=0),并不能判断是否指向合法对象。

likaisoft avatar May 11 '22 04:05 likaisoft