2016-08-12 3 views
0

그래서 함수에 대한 포인터를 전달하고 그 함수가 해당 함수 내부에서 해당 포인터를 수정하도록하고 싶습니다. 어떻게 그 일을 일반적으로합니까? 여기에서?매개 변수를 통해 포인터를 수정하는 방법

편집 : TREENODE 이후

내가 removeLeftMostNode(...) 기능의 내부 nodePtr->vendorDataRef을 어떻게 든 다시 호출 기능을 얻으려면 포인터의 구조체이다. 내가 removeLeftMostNode(...)의 매개 변수를 통해이 작업을 수행 할 수 있다고 생각하므로 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 구조체

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

이며,이에 대한 코드의 일부이다 이진 검색 트리에서 항목을 제거합니다. 이것은 간접적으로 이것을 호출하는 코드입니다.

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; 
      } 
} 
+1

"값으로 전달하지 않으려합니다."거의 모든 매개 변수 (성공 제외)는 값으로 전달됩니다. "포인터에 포인터를 사용하면'removeNode'에서'tempVendorPtr'의 값을 수정하지 않을까 걱정됩니다."왜 두렵습니까? 포인터를 수정합니다. 당신은 명확히 할 수 있습니까? – Rakete1111

+0

죄송하지만 정확히 어떤 문제가 있습니까? 또는 "함수를 가리키는 포인터를 _ 통과시키고 해당 함수를 해당 포인터로 수정하도록하십시오"를 달성하기 위해 무엇을 어디서 시도하고 있습니까? –

+0

참조로 받아 들여야합니다. 오히려 사소한. – EJP

답변

3

어떻게 일반적으로 그렇게합니까?

개체를 수정하려면 개체 또는 개체를 가리키는 포인터로 참조를 전달할 수 있습니다.

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; 
} 
+0

훌륭한 두 번째 예는 제가 찾고있는 것입니다. 나는 그와 같은 것을 시도했다. 그러나 나의 오류는 bar (& ptr) –