[英]How does this operation on pointers work?
int x = 4;
int* q = &x; // Is it always equivalent to int *q = &x; ?
cout << "q = " << q << endl; // output: q = 0xbfdded70
int i = *q; // A
int j = *(int*)q; // B, when is this necessary?
cout << "i = " << i << endl; // output: i = 4
cout << "j = " << j << endl; // output: j = 4
My question is what does lines A and B do, and why the outputs are both 4? 我的问题是A和B行是做什么的,为什么输出都是4?
It is a basic usage of pointers, in A you dereference pointer (access the variable to which a pointer points)": 它是指针的基本用法,在A中,您取消引用指针(访问指针指向的变量)”:
int i = *q; // A
while B is doing exactly the same but it additionally casts pointer to the same type. 虽然B所做的完全相同,但它另外将指针转换为相同的类型。 You could write it like that:
您可以这样写:
int j = *q; // B
there is no need for (int*) 不需要(int *)
int x = 4;
x is 4 x是4
int* q = &x;
q is the memory location of x (which holds 4) q是x 的存储位置 (持有4)
cout << "q = " << q << endl; // output: q = 0xbfdded70
There's your memory location. 有你的记忆位置。
int i = *q; // A
i is the value at memory location q i是内存位置q的值
int j = *(int*)q; // B
j is the value at memory location q. j是内存位置q的值。 q is being cast to an int pointer, but that's what it already is.
q被强制转换为int指针,但这已经是事实了。
int i = *q; // A
Dereferences a pointer to get the pointed value 解引用指针以获取指向的值
int j = *(int*)q; // B
type casts the pointer to an int *
and then dereferences it. 类型将指针强制转换为
int *
,然后取消引用。
Both are same because the pointer is already pointing to an int. 两者都是相同的,因为指针已经指向一个int。 So typecasting to
int *
in second case is not needed at all. 因此,根本不需要在第二种情况下将类型转换为
int *
。
Further derefenecing yields the pointed integer variable value in both cases. 在这两种情况下,进一步的取消预引用都将生成指定的整数变量值。
Lines A and B are equivelent as q is already an int* and therefor (int*)q equals q. A和B行是等价的,因为q已经是int *,因此(int *)q等于q。 int i = *q;
int i = * q; yelds that i becomes the value of the integer pointed to by q.
得出i成为q指向的整数的值。 If you want to make i to be equal to the adress itself remove the asterisk.
如果要使我等于地址本身,请删除星号。
A: Dereference - takes a pointer to a value (variable or object) and returns the value 答:取消引用 -使用指向值(变量或对象)的指针并返回该值
B: Cast to int*
and than dereference B:强制转换为
int*
然后取消引用
The result is the same because the pointer is already to int. 结果是相同的,因为指针已经指向int。 That's it.
而已。
Line A takes the value that q
points to and assigns it to i
. A行采用
q
指向的值并将其分配给i
。 Line b casts q
to the type int*
(which is q
's type already, so that cast is entirely redundant/pointless), then takes the value that q points to and assigns it to j
. 第b行将
q
强制转换为类型int*
(已经是q
的类型,因此强制转换完全是冗余/无意义的),然后获取q指向的值并将其分配给j
。
Both give you 4 because that's the value that q
points to. 两者都给您4,因为那是
q
指向的值。
Line A de-reference pointer q
typed as int *
, ie a pointer points to an int
value. 行A取消引用指针
q
键入为int *
,即,指针指向一个int
值。
Line B cast q
as (int *)
before de-reference, so line B is the same as int j = *q;
行B在取消引用之前将
q
为(int *)
,因此行B与int j = *q;
相同int j = *q;
. 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.