我不知道如何在子功能中使用'kule'。我在代码中的错误标记为 * ** !!! ***。
我必须在函数中写入哪个参数?
void coz(struct ??????,int disk,int * kaynak,int * yardimci,int * hedef)
#define disk_sayisi 5
typedef struct Platform {
int bar1[disk_sayisi];
int bar2[disk_sayisi];
int bar3[disk_sayisi];
} platform;
void kuleleri_ekrana_yaz(platform y);
void disk_no_ata(platform *y);
void disk_tasi(platform *y, int disk);
void coz(int disk, int* kaynak, int* yardimci, int* hedef) {
if (disk > 0) {
printf("Kaynak:\t%d -> Hedef\t%d\n", kaynak, hedef);
*****!!!****** disk_tasi(kule, disk);
*****!!!****** kuleleri_ekrana_yaz(*kule);
}
}
int main() {
platform *kule;
kule = (platform*) malloc(sizeof(platform));
disk_no_ata(kule);
kuleleri_ekrana_yaz(*kule);
coz(disk, kaynak, yardimci, hedef);
return 0;
}
答案 0 :(得分:1)
kule
是函数main
的本地函数,因此您无法在coz
中使用它。如果要在那里使用它,请执行以下任一操作:
(不推荐:)在kule
之外声明main
,并确保在致电coz
之前为其指定有效值:
platform *kule; // removed from `main` to global scope
void coz(…) { … }
int main() {
kule = …;
coz(…);
…
}
(推荐:)将其作为参数传递给coz
(通过附加参数):
void coz(int disk, int* kaynak, int* yardimci, int* hedef, platform *kule) { … }
// ^^^^^^^^^^^^^^^^
int main() { // additional parameter
platform *kule = …;
coz(…, kule);
…
}
答案 1 :(得分:0)
您需要将kule
传递给coz()
:
void coz(int disk, int* kaynak, int* yardimci, int* hedef, platform* kule) {
if (disk > 0) {
printf("Kaynak:\t%d -> Hedef\t%d\n", kaynak, hedef);
disk_tasi(kule, disk);
kuleleri_ekrana_yaz(*kule);
}
}
int main() {
platform *kule;
kule = (platform*) malloc(sizeof(platform));
disk_no_ata(kule);
kuleleri_ekrana_yaz(*kule);
coz(disk, kaynak, yardimci, hedef, kule);
return 0;
}