gpt4 book ai didi

c++ - 这个指针操作是如何工作的?

转载 作者:太空宇宙 更新时间:2023-11-03 10:33:46 25 4
gpt4 key购买 nike

  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?

最佳答案

这是指针的基本用法,在 A 中你解引用指针(访问指针指向的变量)“:

int i = *q; // A

虽然 B 做的完全一样,但它另外将指针转换为相同的类型。你可以这样写:

int j = *q; // B

不需要 (int*)

关于c++ - 这个指针操作是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8681536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com