이 오류를 완전히 이해하지 못합니다. 처음 역 참조 할 때 올바른 값 (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);
@ JoachimPileborg 나는 그것을 생각하고 있었지만 그것이 다른 것이기를 바랐다. 재귀 적으로 함수를 호출 할 때 * dataPointer를 사용하고 3을 추가하면 주소는 3으로 설정됩니다. 물론 & dataPointer를 사용하면 주소를 부여한 다음 큰 숫자를 얻습니다. & 연산자를 사용하지 않고 동일합니다. –