我有一個主程式,它應該作為呼叫函式來加載資料(結構陣列,未定義大小)、正確的資料然后繼續處理它的結果而接收。
以下是我正在嘗試做的一個小例子。該函式loadData接收一個指向主指標的指標,以便主指標可以通過 分配一部分記憶體malloc。資料在loadData函式內加載和列印。但是當它回傳到 main 時,它只顯示結構陣列第一項的正確內容。第二項是垃圾。
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int dni;
char cat;
int weight;
} boxers;
void loadData(boxers *(*xbox), int *xcount)
{
printf("How many boxers? ");
scanf("%d", xcount);
*xbox = (boxers *) malloc(sizeof(boxers) * (*xcount));
for (int i = 0; i < (*xcount); i )
{
printf("Provide the DNI for boxer number %d: ", i);
scanf("%d", &xbox[i]->dni);
printf("Provide the Category for boxer number %d: ", i);
scanf(" %c", &xbox[i]->cat);
printf("Provide the Weight for boxer number %d: ", i);
scanf("%d", &xbox[i]->weight);
}
// First print the result of data loading
for (int i = 0; i < *xcount; i )
{
printf("DNI for boxer number %d, is: %d \n", i, xbox[i]->dni);
printf("Category for boxer number %d, is: %c \n", i, xbox[i]->cat);
printf("Weight for boxer number %d, is: %d \n", i, xbox[i]->weight);
}
}
int main()
{
boxers *box;
int count;
loadData(&box, &count);
// Second print the result of data loading
printf("\n\n");
for (int i = 0; i < count; i )
{
printf("DNI for boxer number %d, is: %d \n", i, box[i].dni);
printf("Category for boxer number %d, is: %c \n", i, box[i].cat);
printf("Weight for boxer number %d, is: %d \n", i, box[i].weight);
}
free(box);
return 0;
}
控制臺輸出如下:
How many boxers? 2
Provide the DNI for boxer number 0: 123
Provide the Category for boxer number 0: A
Provide the Weight for boxer number 0: 45
Provide the DNI for boxer number 1: 789
Provide the Category for boxer number 1: B
Provide the Weight for boxer number 1: 56
DNI for boxer number 0, is: 123
Category for boxer number 0, is: A
Weight for boxer number 0, is: 45
DNI for boxer number 1, is: 789
Category for boxer number 1, is: B
Weight for boxer number 1, is: 56
DNI for boxer number 0, is: 123
Category for boxer number 0, is: A
Weight for boxer number 0, is: 45
DNI for boxer number 1, is: 7471203
Category for boxer number 1, is: x
Weight for boxer number 1, is: 7536756
uj5u.com熱心網友回復:
&xbox[i]->dni是錯的。from 的回傳值malloc被分配給*xbox,因此*xbox指向記憶體,并xbox指向那個指標。
所以記憶體在*xbox。那里的第一個結構是**xbox或者,等效地,(*xbox)[0]。下一個結構是(*xbox)[1],以此類推。所以你想要(*xbox)[i]帶有 index 的結構i。然后,由于那是一個結構,而不是指向它的指標,因此您需要的是.dni,而不是->dni。所以那個成員是(*xbox)[i].dni. 那么它的地址是&(*xbox)[i].dni。
相比之下,既然xbox是一個指向指標的指標,那么xbox[0]就是那個指標,并且xbox[1]是之后的指標。但在那之后沒有指標;xbox只指向一個指標。所以xbox[i]是錯誤的。
在例程中的其他成員參考中也需要進行相同的更改。
為了避免這種容易犯的錯誤,在傳遞指向指標的指標的例程中,可以定義區域指標以簡化參考:
void loadData(boxers **xbox, int *xcount)
{
…
boxers *p = *xbox = malloc(xcount * sizeof *p);
…
scanf("%d", &p[i]->dni);
…
}
uj5u.com熱心網友回復:
這些作業
for (int i = 0; i < (*xcount); i )
{
printf("Provide the DNI for boxer number %d: ", i);
scanf("%d", &xbox[i]->dni);
printf("Provide the Category for boxer number %d: ", i);
scanf(" %c", &xbox[i]->cat);
printf("Provide the Weight for boxer number %d: ", i);
scanf("%d", &xbox[i]->weight);
}
不正確。你必須寫
for (int i = 0; i < (*xcount); i )
{
printf("Provide the DNI for boxer number %d: ", i);
scanf("%d", &( *xbox )[i].dni);
printf("Provide the Category for boxer number %d: ", i);
scanf(" %c", &( *xbox )[i].cat);
printf("Provide the Weight for boxer number %d: ", i);
scanf("%d", &( *xbox )[i].weight);
}
進而
for (int i = 0; i < *xcount; i )
{
printf("DNI for boxer number %d, is: %d \n", i, ( *xbox )[i].dni);
printf("Category for boxer number %d, is: %c \n", i, ( *xbox )[i].cat);
printf("Weight for boxer number %d, is: %d \n", i, ( *xbox )[i].weight);
}
將上述回圈與主回圈中的此回圈進行比較
for (int i = 0; i < count; i )
{
printf("DNI for boxer number %d, is: %d \n", i, box[i].dni);
printf("Category for boxer number %d, is: %c \n", i, box[i].cat);
printf("Weight for boxer number %d, is: %d \n", i, box[i].weight);
}
區別在于,在 main 中,指標box具有型別,boxers *而在函式中,指標xbox具有型別boxers **。因此,您首先需要取消參考函式中的指標( *xbox ),然后像在 main 中一樣應用下標運算子,例如( *xbox )[i].weight
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/314972.html
上一篇:這個擦除鏈表的功能是否正確?
