我是 C++ 新手。我希望两个不指向任何内容的指针被检测为空指针。然而,这只适用于其中之一。这些指针的物理地址有些不同 - 0xe00000001 vs 0x0(这个被正确检测为空指针)。
我编写了以下代码片段:
#include <iostream>
using namespace std;
struct TNode {
TNode* Parent; // Pointer to the parent node
TNode* Left; // Pointer to the left child node
TNode* Right; // Pointer to the right child node
int Key; // Some data
};
int main() {
TNode parent;
parent.Key = 2;
TNode first;
first.Key = 1;
first.Parent = &parent;
parent.Left = &first;
cout << first.Left << endl; // get 0xe00000001 here
cout << first.Right <<endl; // get 0x0
if (first.Right == nullptr) {
cout <<"rnull"<<endl; // rnull
}
if (first.Left == nullptr) {
cout <<"lnull"<<endl; // nothing
}
return 0;
}
这里发生了什么?基本上,我想找到一种方法来检查 first.Left 是否指向任何内容。