本文介绍了运算符的地址是否返回变量引用的值的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在阅读本章有关指针的内容,其中指出:

I'm reading this chapter on pointers and it states the following:

int myvar = 23...;
foo = &myvar;

我的理解是否正确,它是对象new MyStruct()的地址,而不是变量本身?我对变量(而不是它们引用的对象)的存储方式还没有很好的了解,很可能在编译程序时根本不使用它们.

Is my understanding correct that it's the address of an object new MyStruct(), not the variable itself? I don't yet have good understanding of how variables (not objects they reference) are stored, and it's very likely that they are not used at all when program is compiled.

推荐答案

没有对象23...,它是一个值.有一个名为myvar的对象",它有一个地址.如果您为变量分配新值,则该地址不会更改,这是变量本身的属性.

There is no object 23..., that is a value. There is an "object" named myvar, and it has an address. That address doesn't change if you assign a new value to the variable, it's a property of the variable itself.

int var = 23;
int *foo = &var;
var = 35; // foo isn't changed here, it holds the same address.

*foo = 42; // again, foo itself isn't modified

assert(var == 42); // But we modified var through the address in foo


作为旁注,因为您还标记了此C ++.我只想提一下,由于C ++允许您为自己的类重载运算符(包括&),因此整个混合过程中增加了一定的复杂性.但是对于原始变量,使用&将始终产生该变量的地址.


As a side note, because you also tagged this C++. I'll just mention that there is certain complexity added to the whole mix, due to the fact C++ allows you to overload operators (among them &) for your own classes. But for primitive variables, using & will always yield the address of said variable.

这篇关于运算符的地址是否返回变量引用的值的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 02:40