我有一個像下面這樣的結構,我想在 void 函式內分配 structA 的成員,如下所示。基本上想使用指標在 void 函式上分配結構大小,然后在其他函式中使用這個結構指標來訪問欄位。下面是我想如何撰寫代碼的示例,但不清楚指標和結構的用法。在列印函式頭中,我是使用結構指標還是僅使用結構本身?
typedef struct A {
int *x;
int *y;
} A;
void allocateStruct(int sizeN, A *aType);
void printInfo(A aType);
int main() {
A genericA;
allocateStruct(5, genericA);
int x[5] = {2, 3, 4, 5, 6};
int y[5] = {12, 36, 40, 52, 23};
genericA.x = x;
genericA.y = y;
printInfo(genericA);
}
void allocateStruct(int sizeN, A* aType) {
aType.x = (int)malloc(sizeN * sizeof(int));
aType.y = (int)malloc(sizeN * sizeof(int));
}
void printInfo(A genericP) {
......
}
uj5u.com熱心網友回復:
如果我編譯你的代碼
#include <stdlib.h>
typedef struct A {
int *x;
int *y;
} A;
void allocateStruct(int sizeN, A *aType);
void printInfo(A aType);
int main() {
A genericA;
allocateStruct(5, genericA);
int x[5] = {2, 3, 4, 5, 6};
int y[5] = {12, 36, 40, 52, 23};
genericA.x = x;
genericA.y = y;
printInfo(genericA);
}
void allocateStruct(int sizeN, A* aType) {
aType.x = (int)malloc(sizeN * sizeof(int));
aType.y = (int)malloc(sizeN * sizeof(int));
}
void printInfo(A genericP) {
/* */
}
我明白了
main.c: In function 'main':
main.c:13:23: error: incompatible type for argument 2 of 'allocateStruct'
allocateStruct(5, genericA);
^~~~~~~~
所以我調整呼叫以實際給出原型要求的指標。
#include <stdlib.h>
typedef struct A {
int *x;
int *y;
} A;
void allocateStruct(int sizeN, A *aType);
void printInfo(A aType);
int main() {
A genericA;
allocateStruct(5, &genericA);
int x[5] = {2, 3, 4, 5, 6};
int y[5] = {12, 36, 40, 52, 23};
genericA.x = x;
genericA.y = y;
printInfo(genericA);
}
void allocateStruct(int sizeN, A* aType) {
aType.x = (int)malloc(sizeN * sizeof(int));
aType.y = (int)malloc(sizeN * sizeof(int));
}
void printInfo(A genericP) {
/* */
}
我得到:
main.c: In function 'allocateStruct':
main.c:23:10: error: 'aType' is a pointer; did you mean to use '->'?
aType.x = (int)malloc(sizeN * sizeof(int));
^
所以我就是這樣做的:
#include <stdlib.h>
typedef struct A {
int *x;
int *y;
} A;
void allocateStruct(int sizeN, A *aType);
void printInfo(A aType);
int main() {
A genericA;
allocateStruct(5, &genericA);
int x[5] = {2, 3, 4, 5, 6};
int y[5] = {12, 36, 40, 52, 23};
genericA.x = x;
genericA.y = y;
printInfo(genericA);
}
void allocateStruct(int sizeN, A* aType) {
aType->x = (int)malloc(sizeN * sizeof(int));
aType->y = (int)malloc(sizeN * sizeof(int));
}
void printInfo(A genericP) {
/* */
}
我得到:
main.c: In function 'allocateStruct':
main.c:23:16: warning: cast from pointer to integer of different size [-Wpointer-to-int-cast]
aType->x = (int)malloc(sizeN * sizeof(int));
^
所以我按照 C 的一般建議做,而不是強制轉換 malloc:
#include <stdlib.h>
typedef struct A {
int *x;
int *y;
} A;
void allocateStruct(int sizeN, A *aType);
void printInfo(A aType);
int main() {
A genericA;
allocateStruct(5, &genericA);
int x[5] = {2, 3, 4, 5, 6};
int y[5] = {12, 36, 40, 52, 23};
genericA.x = x;
genericA.y = y;
printInfo(genericA);
}
void allocateStruct(int sizeN, A* aType) {
aType->x = malloc(sizeN * sizeof(int));
aType->y = malloc(sizeN * sizeof(int));
}
void printInfo(A genericP) {
/* */
}
結果:沒有錯誤,沒有警告。
現在測驗,使用給定的列印功能原型和我能想到的第一個實作:
#include <stdlib.h>
#include <stdio.h>
typedef struct A {
int *x;
int *y;
} A;
void allocateStruct(int sizeN, A *aType);
void printInfo(A aType);
int main() {
A genericA;
allocateStruct(5, &genericA);
int x[5] = {2, 3, 4, 5, 6};
int y[5] = {12, 36, 40, 52, 23};
genericA.x = x;
genericA.y = y;
printInfo(genericA);
}
void allocateStruct(int sizeN, A* aType) {
aType->x = malloc(sizeN * sizeof(int));
aType->y = malloc(sizeN * sizeof(int));
}
void printInfo(A genericP) {
printf("%i %i\n", genericP.x[0], genericP.y[0] );
}
我得到了令人滿意的輸出:
2 12
或與您提到的替代方法相同的輸出:
void printInfo2(A* genericP) {
printf("%i %i\n", genericP->x[0], genericP->y[0] );
}
稱為 printInfo2(&genericA);.
誠然,正如 yano 正確指出的那樣,這有點太簡單了。它從 main 覆寫分配的指標,這表示記憶體泄漏。
我必須猜測目的,但我假設實際上一些值的復制(而不是指標)是最有意義的。
作為單個值的示例(如果您不填寫 ,則保留大部分分配的記憶體未初始化/* more */),請執行以下操作main():
genericA.x[0] = x[0];
genericA.y[0] = y[0];
/* more */
再次具有相同的輸出。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/409614.html
標籤:
