如何在子函数中使用结构

时间:2013-04-07 12:14:37

标签: c

我不知道如何在子功能中使用'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;
}

2 个答案:

答案 0 :(得分:1)

kule是函数main的本地函数,因此您无法在coz中使用它。如果要在那里使用它,请执行以下任一操作:

  1. (不推荐:)在kule之外声明main,并确保在致电coz之前为其指定有效值:

    platform *kule;  // removed from `main` to global scope
    
    void coz(…) { … }
    
    int main() {
        kule = …;
        coz(…);
        …
    }
    
  2. 推荐:)将其作为参数传递给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;
}