指针上的此操作如何工作? [英] 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
我的问题是A和B行是做什么的,为什么输出都为4?
My question is what does lines A and B do, and why the outputs are both 4?
推荐答案
int x = 4;
x是4
int* q = &x;
q是x(包含4个)的内存位置
q is the memory location of x (which holds 4)
cout << "q = " << q << endl; // output: q = 0xbfdded70
有您的存储位置.
int i = *q; // A
i是内存位置q上的值
i is the value at memory location q
int j = *(int*)q; // B
j是存储器位置q的值. q被强制转换为int指针,但这已经是原来的了.
j is the value at memory location q. q is being cast to an int pointer, but that's what it already is.
这篇关于指针上的此操作如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文