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. So pmyPtr will have 0x0001000 as its assigned a constant pointer.
When pointer is passed to the function func some indexes of array (first two) are updated by DE-referencing.
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.
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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.