简体   繁体   中英

How to modify a pointer through a parameter

So I want to pass a pointer to a function and have that function modify that pointer from inside that function. How do i do that in general? In here?

EDIT:

Since the treeNode is a struct of pointers I want to take nodePtr->vendorDataRef inside of the removeLeftMostNode(...) function and somehow get it back to the calling function. I thought I could do this through a parameter of removeLeftMostNode(...) hence removeLeftMostNode(...aVendor* vendorDataRef)

ie

aBst::treeNode * aBst::removeNode(aBst::treeNode * nodePtr)
{
    aVendor * tempVendorPtr; //<----...to be assigned to this
    treeNode * nodeToConnectPtr, * tempPtr;
    ...
    tempPtr = removeLeftMostNode(nodePtr->right, &tempVendorPtr);
    ...
 }

aBst::treeNode * aBst::removeLeftMostNode(aBst::treeNode * nodePtr, aVendor* vendorDataRef)
{
    if(nodePtr->left == NULL)
    {
        //Target acquired, modify the value of vendorData through parameter
        vendorDataRef = nodePtr->vendorData; //<---I want this pointer... 
        return removeNode(nodePtr);
    }
    else
        return removeLeftMostNode(nodePtr->left, vendorData);
    }

This is the treeNode struct

struct treeNode
{
    aVendor * vendorData;
    treeNode * left;
    treeNode * right;
};

As you might guess, this is a part of the code to remove an entry from a binary search tree. This is the code that calls it indirectly.

bool aBst::remove(char nameOfVendor[])
{
    bool failControl = false;

    removeValue(root, nameOfVendor, failControl);

    return failControl;
}

aBst::treeNode * aBst::removeValue(aBst::treeNode * subTreePtr, char nameOfVendor[], bool& success)
{
    //Note: the subTreePtr should be root in initial call
    treeNode * tmpPtr;
    char name[MAX_CHAR_LENGTH];

    subTreePtr->vendorData->getName(name);

    if(subTreePtr == NULL) //Empty Tree
    {
        success = false;
        return NULL;
    }
    else if(strcmp(name, nameOfVendor) == 0) //Evaluates to true if there is a match
        {
            //Item is in root of subTreePtr
            subTreePtr = removeNode(subTreePtr);
            success = true;
            return subTreePtr;
        }
        else if(strcmp(name, nameOfVendor) < 0) // Go left
            {
                tmpPtr = removeValue(subTreePtr->left, nameOfVendor, success);
                subTreePtr->left = tmpPtr;
                return subTreePtr;
            }
            else // Go Right
            {
                tmpPtr = removeValue(subTreePtr->right, nameOfVendor, success);
                subTreePtr->right = tmpPtr;
                return subTreePtr;
            }
}

How do i do that in general?

To modify any object, be it an object or pointer to object, you can pass a reference to it.

void foo(int& i)
{
   // Assign to i. The change will be visible in the calling function.
   i = 10;
}

void bar(int*& ptr)
{
   // Assign to ptr dynamically allocated memory.
   // The memory will be valid in the calling function.
   ptr = new int[20];
}

int main()
{
   int i;
   int* ptr;

   foo(i); // When the function returns, i will be 10
   bar(ptr);  // When the function returns ptr will point to an array of 10 ints
   ptr[5] = 20;  // OK since memory for ptr was allocated in bar.

   ...

   // Deallocate memory to prevent memory leak.
   delete [] ptr;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM