- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我无法理解 const int* const &alias_for_ptr = ptr;
的含义关于以下内容:
#include <iostream>
using namespace std;
int main() {
int a = 10;
int* ptr = &a;
const int* const &alias_for_ptr = ptr;
ptr = NULL; //or ptr = 0;
if (ptr == alias_for_ptr)
//This should execute but doesn't
cout << "ptr == alias_for_ptr" << endl;
else
//This should NOT execute but DOES
cout << "ptr != alias_for_ptr" << endl;
return 0;
}
为什么 ptr == alias_for_ptr
返回 false,(事实上 alias_for_ptr
保留其旧值 &a
)?我的印象是 alias_for_ptr
将始终具有与 ptr
相同的值(尽管使用 & 符号)并且 const int* const X = Y
仅确保我不能同时更改 X
的值和 X
通过该标识符 X
指向的值。
此外,如果我删除第二个 const
,那么脚本将无法编译,这让我更加困惑。请注意,编译错误是:invalid initialization of reference of type ‘const int*&’ from expression of type ‘int*’
。
最佳答案
您使用的是哪个编译器?我已经在 MSVC++ 2010 下编译,它如您所愿地工作。
我认为正在发生的事情是将一个临时变量分配给一个常量引用,因为“ptr”的类型从“int*”转换为“const int*”。如果删除第一个 const 会发生什么?
编辑:阅读此处了解更多引用资料:Reference initialization in C++ .
关于c++ - 问题 : 'const int * const & alias_for_ptr = ptr;' , 为什么两个标识符有不同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4482242/
这可能是个愚蠢的问题,但我对 C 指针的理解有点问题。当涉及到数组时更是如此。例如: char ptr[100]; ptr[0]=10; fprintf(stderr, "&ptr: %p \n pt
当我尝试 printf("%p","%d","%u",ptr,ptr,ptr) 时,我得到 00405067 但是当我尝试执行 printf("%p %d %u",ptr, ptr,ptr) 我分别得
ffunction glMultiDrawElements 需要一个指向指针的指针作为其参数之一。如何从 StorableArray Int a 获取 Ptr(Ptr a) ? 最佳答案 您需要先将索
最近我遇到了这个我自己无法理解的问题。 这三个表达式有什么用真是什么意思? *ptr++ *++ptr ++*ptr 我试过里奇。但不幸的是,他无法理解他讲述的这 3 次手术。 我知道它们都是为了增加
这个问题已经有答案了: C pointer syntax (4 个回答) 已关闭 7 年前。 我最近在研究C语言中的指针,我似乎无法完全理解这段代码: int *ptr= (*int) 99999;
假设*ptr指向一个变量。 *ptr、&ptr 和 ptr 各自的含义是什么? 很多时候,我对它们感到困惑。有人介意澄清这些陈述并举一些具体的例子吗? 最佳答案 在函数中采用以下变量。 int i =
这个问题在这里已经有了答案: Why in a 2D array a and *a point to same address? [duplicate] (4 个答案) 关闭 3 年前。 我正在测试
我在一次采访中看到了这段代码。 int main() { int **p; p = (int **) new int(7); cout<<*p; return 0; }
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: ++ on a dereferenced pointer in C? 同样,什么会*指针+= 1*ptr %
++*ptr++ 中的求值顺序是什么?当操作涉及指针和左值时它会改变吗? 如果a++的优先级高于*a或++a,那么为什么是++*a++ 评估为首先返回递增的值然后更改指针,而不是更改指针,然后递增该位
假设*ptr 指向一个变量。 *ptr、&ptr 和 ptr 分别是什么意思? 很多时候,我对它们感到困惑。有人介意澄清这些陈述并举一些具体的例子吗? 最佳答案 在函数中获取以下变量。 int i =
我是 C++ 的新手,最近我花了几天时间阅读有关指针的内容。我意识到下面的 2 段代码给我不同的结果,尽管它们看起来相同。 第一段代码: int a = 5; int* ptr = &a; cout
我有以下代码,完全可以 100% 正常运行,没有错误、编译或运行时。但这太难看了,因为当我确定有办法不需要其中任何一个时,我必须强制转换并使用一个无关的变量。 structMSGB ***init_b
我不太确定 ptr&&(*ptr) 的使用 起初,我不确定这是为了检查 NULL ptr 还是赋值是否存在。 但是现在运行下面的代码后我发现这两种方式都不起作用:发生运行时错误! 有人能解释一下里面发
示例#1 #include struct node{ struct node *next; char *data; }; struct node *reverse_list(stru
我正在尝试使用两种方法删除示例二叉搜索树的左子节点 (10): 方法 1:通过将指针传递给指向当前节点的指针。 方法2:通过将指针的地址传递给当前节点。这不会删除节点,但调用 delete 会破坏指针
我一直在研究 C 中的字符串,并且在以下代码中遇到了以下问题: #include int main() { char *p = "foo"; printf("%p\t%p\t%p",&p
如果我这样做: int x = 10; void *ptr = &x; ptr++; “ptr++”行给出错误。 但是,如果我这样做而不是“ptr++”: ptr = ptr + 1; 它工作得很好。
我正在学习C语言,并且很困惑++*ptr和*ptr++之间的区别。 例如: int x = 19; int *ptr = &x; 我知道 ++*ptr 和 *ptr++ 会产生不同的结果,但我不确定为
我正在学习C语言,并且很困惑++*ptr和*ptr++之间的区别。 例如: int x = 19; int *ptr = &x; 我知道 ++*ptr 和 *ptr++ 会产生不同的结果,但我不确定为
我是一名优秀的程序员,十分优秀!