这小段代码帮助理解下指针和对象的大小,& 和*的作用
#include <stdio.h>
#include<iostream>
#define TElemType int
//构造结点的结构体
typedef struct BiTNode
{
TElemType data;//数据域
struct BiTNode *lchild, *rchild;//左右孩子指针
}BiTNode, *BiTree;
int main()
{
std::cout << "sizeof(BiTNode) = "<<sizeof(BiTNode) << std::endl;
std::cout << "sizeof(BiTree) = " << sizeof(BiTree) << std::endl;
BiTree Tree;
std::cout <<"#####################" << std::endl;
std::cout << "sizeof(Tree) = " << sizeof(Tree) << std::endl;
std::cout << "sizeof(&Tree) = " << sizeof(&Tree) << std::endl;
std::cout << "sizeof(*Tree) = " << sizeof(*Tree) << std::endl;
std::cin.get();
return 0;
}
答案是:
sizeof(BiTNode) = 12
sizeof(BiTree) = 4
#####################
sizeof(Tree) = 4
sizeof(&Tree) = 4
sizeof(*Tree) = 12