x =& v和** = v是否等效? [英] Are `x = &v` and `*x = v` equivalent?

查看:83
本文介绍了x =& v和** = v是否等效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int * x;
int v = 7;

给出此代码,
1有什么区别。 x =& v
2。 * x = v
我了解在两种情况下, * x 包含 7 ,但 x 是否在两种情况下都包含 v 的内存位置?如果不是,在情况1和2中 x 包含什么,这是两者之间唯一的显着区别吗?

Given this code, what is the difference between 1. x = &v , and 2. *x = v ? I understand that in both cases, *x contains 7 but does x contain memory location of v in both cases? If not, what does x contain in cases 1 and 2, and is this the only significant difference between the two?

推荐答案

给出以下语句:

int v = 7; 

v 在内存中有一些位置。这样做:

v has some location in memory. Doing:

x = &v;

将指向 x v 的位置,实际上 * x 的位置值将为 7

will "point" x to the memory location of v, and indeed *x will have the value 7.

但是,在此语句中:

*x = v;

您将 v 的值存储在 x 指向的地址。但是 x not 指向有效的内存地址,因此该语句调用了未定义的行为。

you are storing the value of v at the address pointed at by x. But x is not pointing at a valid memory address, and so this statement invokes undefined behavior.

所以要回答您的问题,,这2条语句不相等。

So to answer your question, no, the 2 statements are not equivalent.

这篇关于x =& v和** = v是否等效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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