&pA
函数之前和之后的代码foo
中的必须不同,但它是相同的,因此A
变为null
。值得一提的是result_Mat
中的foo
有值,所以ppA
不是null
。为什么A
变为空以及如何解决?
void main()
{
// some code
Mat A;
uchar* pA = A.data;
cout << &pA << endl;
foo(&pA);
cout << &pA << endl;
// some code
}
void foo(uchar** ppA)
{
Mat result_Mat;
// some code
*ppA= result_Mat.data;
// some code
}
答案 0 :(得分:2)
void main()
{
Mat A;
uchar* pA = A.data; // pA points to A.data, let's say it's 0xAAAAAAAA
cout << &pA << endl; // Let's say &pA is 0xBBBBBBBB
foo(&pA); // Call to foo with pA's address, 0xBBBBBBBB
void foo(uchar** ppA) // ppA points to pA's address, 0xBBBBBBBB
// If you dereference ppA it will give you the value of
// the pointer at 0xBBBBBBBB, which is 0xAAAAAAAA
{
Mat result_Mat;
*ppA= result_Mat.data; // You're changing the original pA pointer value
// from 0xAAAAAAAA to address of result_Mat.data,
// let's say for example 0xCCCCCCCC.
}
// Back in the main function
cout << &pA; // &pA hasn't changed. Value of pA used to be 0xAAAAAAAA, you
// sent it to the foo function and changed its value to 0xCCCCCCCC.
}
指针所在的实际地址,换句话说,它可能需要4或8个字节来存储&#34;某处的地址是&amp; pA,它从未改变过。它无法更改,因为您将其声明为堆栈上的任何其他变量,并且这些局部变量不会更改其地址。
答案 1 :(得分:1)
这可能会有所帮助:
正如你所说
在poo函数之前和之后的代码&amp; pA中的必须是不同的
但是如果你试图改变foo函数中同一个指针pA
的内容,那么为什么&pA
(即pA
)的地址会发生变化。
我认为您正在寻找*
,即dereferenc
运算符来检查指针指向的值