C++中的(unsigned int)&代表的意思

时间:2014-07-02 08:13:52   收藏:0   阅读:257

int  main()

{
    using namespace std;
    int i = 20;
    int m = (unsigned int)&i; //注意这里的&符号
    cout  << m << endl;
}

&:表示引用的意思,表示m是i的一个别名,相当于人的小名。


再看下面的例子:

#include <stdio.h>

#define FIND(struc,e)  (int)&(((struc *)0)->e)

typedef struct {
	int  a;
	char b[20];
	double ccc;
}stu;

int main()
{
	printf("%d\n",FIND(stu,b[0]));
	printf("%d\n",FIND(stu,ccc));

	return 0;
}

这里定义了一个宏FIND,求结构体struc里的某个变量相对于struc的偏移量。

其中(struc *)0表示将常量0强制转化为struc *型指针所指向的地址;

&(((struc &)0)->e)表示取结构体指针(struc *)0的成员e的地址。

输出结果为:4   24


C++中的(unsigned int)&代表的意思,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!