2014-11-26 3 views
1

이 오류를 완전히 이해하지 못합니다. 처음 역 참조 할 때 올바른 값 (0)입니다. 그러나 두 번째 반복에서 변수의 주소는 임의의 주소로 설정됩니다 (무작위가 아니므로 포인터 값에 추가하는 대신 현재 포인터에 3을 더하는 것입니다).참조로 전달 된 포인터 수정

void constructTree(const unsigned int data[], const unsigned int dataSize, unsigned  int *dataPointer, node* currentNode) 
{ 
//If the pointer is out of bounds, the tree has been built 
if (*dataPointer > dataSize) return; 
//If the dataPointer is pointing to the id of the current node 

if (data[*dataPointer] == currentNode->m_id) 
{ 
    printf("%d, ", currentNode->m_id); 
    //Create the left and right nodes 
    if (data[*dataPointer + 1] != 0) { 
     currentNode->m_left = (node*)malloc(sizeof(node)); 
     currentNode->m_left->m_id = data[*dataPointer + 1]; 
     currentNode->m_left->m_left = NULL; 
     currentNode->m_left->m_right = NULL; 
     currentNode->m_left->m_parent = NULL; 
    } 
    if (data[*dataPointer + 2] != 0) { 
     currentNode->m_right = (node*)malloc(sizeof(node)); 
     currentNode->m_right->m_id = data[*dataPointer + 2]; 
     currentNode->m_right->m_left = NULL; 
     currentNode->m_right->m_right = NULL; 
     currentNode->m_right->m_parent = NULL; 
    } 

    printf("%d", *dataPointer); 
    constructTree(data, dataSize, &*dataPointer + 3, currentNode->m_left); 
    constructTree(data, dataSize, &*dataPointer + 3, currentNode->m_right); 

} 


} 

호출이 기능 : 당신이 원하는 경우

int datatemp = *dataPointer + 3 ; 

constructTree(data, dataSize, &datatemp , currentNode->m_left); 
constructTree(data, dataSize, &datatemp , currentNode->m_right); 

: 난 당신이 임시 변수를 원래 값을 변경할 수 있으므로 사용하지 않는 있으리라 믿고있어

unsigned int dataPointer = 0; 
    constructTree(vector, vectorSize, &dataPointer, head); 
+0

@ JoachimPileborg 나는 그것을 생각하고 있었지만 그것이 다른 것이기를 바랐다. 재귀 적으로 함수를 호출 할 때 * dataPointer를 사용하고 3을 추가하면 주소는 3으로 설정됩니다. 물론 & dataPointer를 사용하면 주소를 부여한 다음 큰 숫자를 얻습니다. & 연산자를 사용하지 않고 동일합니다. –

답변

2

변경하려면 먼저 포인터를 변경 한 다음 포인터를 전달하십시오.

*dataPointer = *dataPointer + 3 ; 

constructTree(data, dataSize, dataPointer , currentNode->m_left); 
constructTree(data, dataSize, dataPointer , currentNode->m_right); 
+0

반대로 나는 원래 값을 변경하고 싶습니다. –

+0

@DanielMartin 두 옵션을 모두 작성했습니다. – 2501

0

실제로 문제 : 첫 번째 이유는 operator precedence입니다. address-of (&) 연산자와 참조 해제 (*) 연산자는 모두 더하기 연산자보다 우선 순위가 높기 때문에 &*dataPointer + 3(&*dataPointer) + 3과 같으며 주소 및 참조 연산자가 서로를 취소하기 때문에 dataPointer + 3과 같습니다.

두 번째 문제는 표현식의 주소를 사용할 수 없다는 것입니다. 임시 변수에 표현식을 저장하고 해당 변수에 주소를 전달해야합니다. 또는 원래 값을 수정하려면 포인터를 전달하십시오.

그러니 실제로 아마 당신은 당신의 용어가 혼동있어 한 참조로 전달하는 지원하지 않습니다

int temp = *dataPointer + 3; 
constructTree(data, dataSize, &temp, currentNode->m_left); 
constructTree(data, dataSize, &temp, currentNode->m_right); 

또는

*dataPointer += 3; 
constructTree(data, dataSize, dataPointer , currentNode->m_left); 
constructTree(data, dataSize, dataPointer , currentNode->m_right); 
0

C? 나는 문제가 당신의 라인에 생각 :

constructTree(data, dataSize, &*dataPointer + 3, currentNode->m_right) 

난 당신이 생각하고 이해한다면, 당신은이 dataPointer 플러스 3에서 해결하는 값의 주소를 통과 기대하고 무엇 실제로을 통과하고있어 dataPointer 의해 어드레싱 포인터 간접 참조 포인터의 주소 (dataPointer) + 3

  • dataPointer = 포인터
  • dataPointer * 값 =
  • & * = 추가 dataPointer dataPointer에 의해 처리되는 값의 ress