简体   繁体   English

我在 c 中编译这个结构程序时出错

[英]I get an error compiling this struct program in c

#include <stdio.h>
#include <stdlib.h>

struct date {
    int day;
    int month;
    int year;
};

struct lottery {
    int aa;
    struct date date1;
    int n1;
    int n2;
    int n3;
    int n4;
    int n5;
    int joker;
};

void load(struct lottery *array) {
    FILE *fp;
    fp = fopen("as.txt", "r");
    if (fp == NULL)
        printf("00000\n");
    int i;
    for (i = 0; i < 1; i++) {
        fscanf(fp, "%d;%d/%d/%d;%d;%d;%d;%d;%d;%d", &array[i].aa, &array[i].date1.day, &array[i].date1.month, &array[i].date1.year, &array[i].n1, &array[i].n2, &array[i].n3, &array[i].n4, &array[i].n5, &array[i].joker);
        if (feof(fp))
            break;
    }
    array = (struct lottery*)realloc(array, i * sizeof(struct lottery));
    //  printf("%d;%d/%d/%d;%d;%d;%d;%d;%d;%d", array[0].aa, array[0].date1.day, array[0].date1.month, array[0].date1.year, array[0].n1, array[0].n2, array[0].n3, array[0].n4, array[0].n5, array[0].joker);
 }

int main() {
    struct lottery *array;
    array = (struct lottery *)malloc(4 * sizeof(struct lottery));
    // printf("%d", sizeof(struct lottery));
    load(struct lottery array);
    printf("%d",array[0].aa);

    return 0;
}

Hello, I get an error at the line load(struct lottery array);你好,我在行load(struct lottery array);出错load(struct lottery array); in my main function.在我的main功能中。 The error says Expected expression before struct .错误显示struct 之前的 Expected expression I googled it and I couldn't understand why would it expect an expression there and I am kinda confused.我用谷歌搜索了它,我不明白为什么它会在那里期待一个表达,我有点困惑。

The problem is with the function call.问题在于函数调用。 In your code, you've written在你的代码中,你写了

 load(struct lottery array);

which is wrong.这是错误的。 You should pass only the variable as the argument, like你应该只传递变量作为参数,比如

load(array);

That said, your fopen() failure check and subsequent code also looks wrong.也就是说,您的fopen()失败检查和后续代码看起来也有误。 You're checking for the failure case but continue to use the returned pointer anyway, which kinds of nullifies the effect of having the check in the first place.您正在检查失败情况,但无论如何继续使用返回的指针,这会使首先进行检查的效果无效。

Parameters passed to functions when calling them are just variables - you don't need to include the data types as well.调用函数时传递给函数的参数只是变量 - 您也不需要包含数据类型。 So the line所以线

load(struct lottery array);

should just be应该只是

load(array);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM