大家!真的,我只是想了解這些參考和指標發生了什么。你能解釋一下,為什么在一種情況下一切都很好,但在另一種情況下我什么也沒收到,我將從作業案例開始。所以,有一個程式(這不是我的):
#include <iostream>
#include <conio.h>
using namespace std;
struct node{
double data;
node *left;
node *right;
};
node *tree = NULL;
void push(int a, node **t)
{
if ((*t) == NULL)
{
(*t) = new node;
(*t)->data = a;
(*t)->left = (*t)->right = NULL;
return;
}
if (a > (*t)->data) push(a, &(*t)->right);
else push(a, &(*t)->left);
}
void print (node *t, int u)
{
if (t == NULL) return;
else
{
print(t->left, u);
for (int i=0; i<u; i) cout << "|";
cout << t->data << endl;
u--;
}
print(t->right, u);
}
int main ()
{
int n;
int s;
cout << "Enter the amount of elements ";
cin >> n;
for (int i=0; i<n; i)
{
cout << "Enter the value ";
cin >> s;
push(s, &tree);
}
cout << "Your binary tree\n";
print(tree, 0);
cin.ignore().get();
}
這是由結構、指標和參考組成的二叉搜索樹。它作業得很好但是如果我以下面的方式修改程式它不起作用。我不明白為什么,因為
int *tree;
int **treePointer;
cout << (tree = *treePointer) <<endl; // Shows 1 i.e. true
修改后的代碼:
#include <iostream>
#include <conio.h>
using namespace std;
struct node{
double data;
node *left;
node *right;
};
node *tree = NULL;
void push(int a, node *t)
{
if ((t) == NULL)
{
(t) = new node;
(t)->data = a;
(t)->left = (t)->right = NULL;
return;
}
if (a > (t)->data) push(a, (t)->right);
else push(a, (t)->left);
}
void print (node *t, int u)
{
if (t == NULL) return;
else
{
print(t->left, u);
for (int i=0; i<u; i) cout << "|";
cout << t->data << endl;
u--;
}
print(t->right, u);
}
int main ()
{
int n;
int s;
cout << "Enter the amount of elements ";
cin >> n;
for (int i=0; i<n; i)
{
cout << "Enter the value ";
cin >> s;
push(s, tree);
}
cout << "Your binary tree\n";
print(tree, 0);
cin.ignore().get();
}
如您所見,所有更改都發生在 push 函式引數中。為什么它不作業?
我希望原始程式和修改后的程式可以相同
uj5u.com熱心網友回復:
發生這種情況是因為修改后的代碼中的以下行
(t) = new node;
您正在為函式呼叫中創建的指標分配記憶體,該記憶體將在函式呼叫后丟失。
請注意,如果您在函式呼叫中對結構的成員進行更改,則更改將反映在整個程序中,因為在這種情況下,您將通過參考來處理結構的成員。
但是將指標指向在函式呼叫中創建的新記憶體位置不會傳播更改。您還需要通過參考傳遞指標(因此指標指向指標)。
考慮以下示例 -
#include <iostream>
typedef struct exampleStruct
{
int a;
} exampleStruct;
void changeStruct(exampleStruct* S, int changedValue)
{
S = new exampleStruct;
S->a = changedValue;
printf("Value of a inside Function = %d\n", S->a);
}
void changeStruct(exampleStruct** S, int changedValue)
{
(*S) = new exampleStruct;
(*S)->a = changedValue;
}
int main()
{
exampleStruct* S = new exampleStruct;
S->a = 100;
printf("Value of a before calling function = %d\n", S->a);
changeStruct(S, 900);
printf("Value of a after calling the function = %d\n", S->a);
changeStruct(&S, 1300);
printf("Value of a after calling 2nd function = %d\n", S->a);
}
輸出如下 -
Value of a before calling function = 100
Value of a inside Function = 900
Value of a after calling the function = 100
Value of a after calling 2nd function = 1300
在上面的示例中,與修改后的代碼類似,第一個函式無法傳播更改,而使用指向指標的指標可以解決問題
uj5u.com熱心網友回復:
在修改后的代碼中,當你這樣做
(t) = new node;
您正在將區域變數 t 的值設定為指向新節點。這個值不會放在你的樹中,它只存在于 push 函式中。
在原來的
(*t) = new node;
將 t 指向的變數設定為新節點的地址。這意味著樹被修改。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/521920.html
標籤:C 指针参考
