有人可以帮我理解这个吗? int *& pr [英] Can someone help me understand this? int *& pr
本文介绍了有人可以帮我理解这个吗? int *& pr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在最后的考试中发现了这一点:
int a = 564;
int * pa =& a;
int *& pr = pa;
cout<< * pr;
根据多项选择答案,代码有效,并显示a的值。 p>
但是我对第3行的评估和优先级感到困惑。C的操作顺序表示*和&具有相同的顺序。那么,它会是 int *(& pr)
?
谢谢。
解决方案
它是一个指针的引用。在C中,您可以将其表示为指向指针的指针。
您可以这样写:
// C ++ style
void update_my_ptr(int *& ptr){ptr = new int [1024] }
// C style
void update_my_ptr_c(int ** ptr){* ptr = malloc(1024 * sizeof(int)); }
int main()
{
int * ptr;}
update_my_ptr(ptr);
//这里ptr被分配!
}
I found this in a final exam:
int a = 564;
int* pa = &a;
int *& pr = pa;
cout << *pr;
According to the multiple choice answer, the code is valid, and displays the value of a.
But I'm confused about evaluation and precedence for line 3. Order of operations for C states that * and & have the same order. So, would it then be int *(&pr)
? How can this be described in words?
Thank you.
解决方案
It's a reference to a pointer. In C, you would express that as a pointer to a pointer.
You could write something like this:
// C++ style
void update_my_ptr(int*& ptr) { ptr = new int[1024]; }
// C style
void update_my_ptr_c(int **ptr) { *ptr = malloc(1024 * sizeof(int)); }
int main()
{
int *ptr;
update_my_ptr(ptr);
// Here ptr is allocated!
}
这篇关于有人可以帮我理解这个吗? int *& pr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文