#include <stdio.h>
#include <stdlib.h>
void input(int **ar, int *n) {
printf("Number of elements: ");
scanf("%d", n);
*ar = (int*)malloc(*n * sizeof(int));
//*ar = new int[*n];
for (int i = 0; i < *n; i){
printf("Element %d:", i);
scanf("%d", &(*ar)[i]);
}
return;
}
void output(int *ar, int n) {
for (int i = 0; i < n; i) {
printf("]\n", ar[i]);
}
return;
}
int main() {
int n = 0;
int *ar = NULL;
input(&ar, &n);
output(ar, n);
free(ar);
return 0;
}
所以我剛剛在我的大學學習了 C 語言中的指標。有些我明白,有些我不明白。例如,我不明白為什么函式中是 anar而不是. 我的意思是我嘗試替換并洗掉函式中的in并且沒有編譯錯誤。當我運行程式時,一切正常。我輸入元素的數量并在這些元素中輸入一些值,但是當我在最后一個元素中輸入最后一個數字時,我什么也得不到。所以我解開了一切,程式再次正常作業。inputint**int*int** arint* ar*int* ar = NULLmain
uj5u.com熱心網友回復:
您發布的程式正在執行以下操作(按以下給定順序):
- 在函式中宣告/定義指標
- 在其他函式中為該指標分配記憶體并用用戶輸入填充該記憶體
- 使用另一個函式列印該記憶體的內容
- 釋放記憶體并退出
讓你困惑的部分是2。
我不明白為什么輸入函式中的 ar 是 int** 而不是 int* ....
如果您想在其他函式中為指標分配記憶體(或想要更改指標,例如使其指向其他記憶體或重新分配記憶體等),則需要在該函式中訪問該指標,您可以如果您有該指標的地址,則可以訪問。
這就是為什么在這里ar傳遞指標的地址:
input(&ar, &n);
將地址傳遞ar給input()函式并在輸入函式中取消參考該地址(即*ar)將給出ar指標(在 中宣告/定義main())。
為了更好地理解,我將函式的ar引數重命名為:input()input_ar
void input(int *input_ar, int *n) {
.....
.....
當input()從函式呼叫main()函式時,記憶體中的視圖將是這樣的:
input_ar ar
----- -----
| 200 | ----> | NULL|
----- -----
[200 is address of ar pointer]
在input()功能中,當您執行以下操作時:
*input_ar = (int*)malloc(*n * sizeof(int));
取消參考input_ar(即*input_ar)將給出ar:
input_ar *input_ar (ar) (newly allocated memory block)
----- ----- -----
| 200 | ---------> | 300 | ----> | |
----- ----- -----
[300 is address of the newly allocated memory block]
因此,通過取消參考input_ar,指標ar可以在 中更改input()。
我的意思是我嘗試用 int* ar 替換 int** ar 并洗掉 main 函式中的 * in int* ar = NULL 并且沒有編譯錯誤。
請注意,如果您洗掉 in 的from*宣告,則將是 type 的變數,這意味著它將不是指標,而是可以保存型別值的變數。如果您在程式中進行此更改,您還必須在其他地方進行更改,例如您在功能中從用戶那里獲取輸入的方式,功能中的相應更改等。armain()arintintinput()output()
You have not shown the program with the changes that you have done after deleting * from ar declaration so, I will just talk about the ar argument passed to input():
This is what you might have tried:
int ar;
input (&ar, &n);
and in input():
void input(int *ar, int *n) {
printf("Number of elements: ");
scanf("%d", n);
ar = (int*)malloc(*n * sizeof(int));
.....
Note that the ar pointer parameter is a local variable of input() function.
When input() function is called from main() function, the ar will hold the address of ar variable of main() function.
If you want, you can make changes in the value of ar variable using the pointer ar in input() function, e.g. - *ar = 9;
This will assign value 9 to variable ar (of main()). The in-memory view would be something like this
ar (of input()) ar (of main())
----- -----
| 200 | -------------> | |
----- -----
[200 is address of ar variable of main()]
But when you do
ar = (int*)malloc(*n * sizeof(int));
ar已經丟失了函式變數的地址,并且ar新main()分配的記憶體參考被分配給 的指標ar,input()即
ar (of input()) newly allocated memory block
----- -----
| 400 | -------------> | |
----- -----
[400 is address of the newly allocated memory block]
您可以寫入/修改此記憶體中的內容,但是當從 中回傳時input(),此記憶體參考將丟失,因為input()沒有回傳任何內容,這將是您的程式中的記憶體泄漏。
uj5u.com熱心網友回復:
此處使用的指標的一個常見用途是允許函式呼叫更改其中一個變數的值。
例如,假設您有以下內容:
void
change_num(int num)
{
num = 6;
}
int
main()
{
int num = 5;
change_num(num);
printf("%i\n", num);
return 0;
}
這不起作用,因為change_num正在更改它自己的副本而num不是num它自己。
相反,您必須傳遞一個指向num該函式的指標:
void
change_num(int* num_ptr)
{
*num_ptr = 6;
}
int
main()
{
int num = 5;
change_num(&num);
printf("%i\n", num);
return 0;
}
該行的*num_ptr = 6;意思是“將 指向的物件更改num_ptr為 6”。
在一般情況下,如果我們想要一個函式改變型別變數的值SomeType,那么我們必須將 a 傳遞SomeType*給函式。
在您的情況下,您想更改 的值ar,即int*. 現在,它不保存任何東西的地址 ( NULL),您希望將其更改為保存已分配陣列的值(由 創建malloc)。如果我們將 an 傳遞int*給函式以更改 anint并且我們傳遞 aSomeType*以更改 a SomeType,那么我們必須傳遞 anint**以更改 an int*。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/441814.html
