[英]Updating pointer address in function, when passed as an argument. [ passing pointer as reference in C]
CONCEPT: Passing pointer by reference 概念:通过引用传递指针
Trying to achieve: To get updated pointer address from function, when passed as an argument. 尝试实现:当作为参数传递时,要从函数获取更新的指针地址。
int main(void)
{
uint8_t unArray[10] = {0}; // unint8_t is type def as unsigned char
uint8_t * pmyPtr;
pmyPtr = unArray;
func(pmyPtr);
*pmyPtr = someValue3;
}
void func(uint8_t * unPtr)
{
*unPtr = someValue1;
unPtr++;
*unPtr = someValue2;
unPtr++;
}
Suppose we have unArray address as 0x0001000. 假设我们的unArray地址为0x0001000。 So pmyPtr will have 0x0001000 as its assigned a constant pointer.
因此,pmyPtr将为0x0001000分配一个常量指针。
When pointer is passed to the function func some indexes of array (first two) are updated by DE-referencing. 当指针传递给函数func时,数组的某些索引(前两个)通过DE引用进行更新。
When I come back to the main after func execution I am trying to update the third index. 函数执行后回到主目录时,我正在尝试更新第三个索引。 How can this be achieved.
如何做到这一点。 I have a hunch that double De-referencing might be handy.
我预感双重取消引用可能会派上用场。
You are correct. 你是对的。 A pointer to pointer is a simple solution.
指向指针的指针是一种简单的解决方案。 In c++ it could be syntactically hidden as a reference.
在c ++中,它可以在语法上隐藏为参考。
main()
{
uint8_t unArray[10] = {0}; // unint8_t is type def as unsigned char
uint8_t * myPtr;
myPtr = unArray;
func(&myPtr);//NOTICE ADDRESS TAKING
*myPtr = someValue3;
}
void func(uint8_t ** unPtrPtr)//ONE MORE STAR
{
uint8_t * unPtr=*unPtrPtr;//CHANGED
*unPtr = someValue1;
unPtr++;
*unPtr = someValue2;
unPtr++;
*unPtrPtr = unPtr;//CHANGED
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.