变量地址的C地址 [英] C address of an address of a variable

查看:110
本文介绍了变量地址的C地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取C语言中地址的地址的唯一方法(与双重引用相反,具有中间变量吗?

Is the only way of getting the address of an address in C (opposite to double dereference to have an intermediate variable?

例如我有:

int a;
int b;
int *ptr_a;
int *ptr_b;
int **ptr_ptr_a;

a = 1;
ptr_a = &a;
ptr_ptr_a = &(&a);   <- compiler says no
ptr_ptr_a = &&a;     <- still compiler says no
ptr__ptr_a = &ptr_a; <- ok but needs intermediate variable

但是您可以做相反的操作,例如

but you can do the inverse, e.g.

b = **ptr_ptr_a;     <- no intermediate variable required

例如我不必这样做:

ptr_b = *ptr_ptr_b;
b = *ptr_b;

为什么两个运算符的功能不对称?

why are the two operators not symmetrical in their functionality?

推荐答案

address-of运算符返回一个右值,并且您不能使用右值的地址(请参阅

The address-of operator returns an rvalue, and you cannot take the address of an rvalue (see here for an explanation of the differences between rvalues and lvalues).

因此,您必须通过将其存储在变量中将其转换为左值,然后才能获取该变量的地址.

Therefore, you have to convert it into an lvalue by storing it in a variable, then you can take the address of that variable.

这篇关于变量地址的C地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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