如何通过参数修改指针 [英] 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?
由于treeNode是指针的结构,因此我想将nodePtr->vendorDataRef
放在removeLeftMostNode(...)
函数内部,并以某种方式将其返回给调用函数.我以为可以通过参数removeLeftMostNode(...)
来做到这一点,因此removeLeftMostNode(...aVendor* vendorDataRef)
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)
即
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);
}
这是treeNode结构
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;
}
这篇关于如何通过参数修改指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!