Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何在函数中传递结构数组? 准备好放弃了吗_C_Struct_Expression - Fatal编程技术网

C 如何在函数中传递结构数组? 准备好放弃了吗

C 如何在函数中传递结构数组? 准备好放弃了吗,c,struct,expression,C,Struct,Expression,几个小时来我的代码一直有问题,一直告诉我有一个关于错误的错误:在“Robot_t”之前需要表达式,但找不到解决方案,如果有人有一个有效的解决方案,你会救我的 这是提供的错误消息 Arrayintofn.c: In function 'main': Arrayintofn.c:23:23: error: expected expression before 'Robot_t' loading_Profiles (Robot_t RobotInfo[]); 无论我做什么或咨询谁,都没有解

几个小时来我的代码一直有问题,一直告诉我有一个关于错误的错误:在“Robot_t”之前需要表达式,但找不到解决方案,如果有人有一个有效的解决方案,你会救我的 这是提供的错误消息

Arrayintofn.c: In function 'main':
Arrayintofn.c:23:23: error: expected expression before 'Robot_t'
     loading_Profiles (Robot_t RobotInfo[]);
无论我做什么或咨询谁,都没有解决办法

下面是代码

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>

typedef struct
  {
    int Robot_Number;
    char Robot_Name[30];
    int Year_Manufacturer;
    float Top_Speed;
    float Mass;
    float Best_Score;
} Robot_t;

void loading_Profiles();

int main()
{
    Robot_t RobotInfo[5];

    loading_Profiles (Robot_t RobotInfo[]);

    int i;
    for (i = 0; i < 50; i++) {
 
        printf("%d\t\t%s\t\t%d\t\t\t%.2f\t\t%.2f\t\t%.2f\n",
                RobotInfo[i].Robot_Number, RobotInfo[i].Robot_Name,
               RobotInfo[i].Year_Manufacturer, RobotInfo[i].Top_Speed, 
               RobotInfo[i].Mass, RobotInfo[i].Best_Score);
 
    }
 
    return 0;
}

void loading_Profiles()
{
    int Counter = 0;
    int i;

    Robot_t RobotInfo[5];

    FILE *ROBOTtxt = fopen("Robot.txt", "r");

    if (ROBOTtxt == NULL) {
        perror("an error occured during the loading of the file\n");
        exit(-1);
    }

    for (i = 0; i < 50; i++) {
        char LineNumber[100] = "";
        fgets(LineNumber, 100, ROBOTtxt);
        sscanf(LineNumber, "%d %s %d %f %f %f",
                &RobotInfo[i].Robot_Number,
                RobotInfo[i].Robot_Name,
                &RobotInfo[i].Year_Manufacturer,
                &RobotInfo[i].Top_Speed,
                &RobotInfo[i].Mass,
                &RobotInfo[i].Best_Score);

        Counter++;

        if (feof(ROBOTtxt)) {
            break;
        }
    }

    if (ferror(ROBOTtxt)) {
        perror("an error has occured");
        exit(-1);
    }

    fclose(ROBOTtxt);
}
#包括
#包括
#包括
#包括
#包括
类型定义结构
{
整数;
char Robot_名称[30];
国际汽车制造商;
浮顶速度;
浮体;
浮动最佳分数;
}机器人;
无效加载_剖面();
int main()
{
Robot_t RobotInfo[5];
加载配置文件(Robot_t RobotInfo[]);
int i;
对于(i=0;i<50;i++){
printf(“%d\t\t%s\t\t%d\t\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n”,
机器人信息[i]。机器人信息编号,机器人信息[i]。机器人名称,
机器人信息[i]。年度制造商,机器人信息[i]。最高速度,
机器人信息[i]。质量,机器人信息[i]。最佳成绩);
}
返回0;
}
无效加载_配置文件()
{
int计数器=0;
int i;
Robot_t RobotInfo[5];
文件*ROBOTtxt=fopen(“Robot.txt”、“r”);
if(robotText==NULL){
perror(“加载文件时出错\n”);
出口(-1);
}
对于(i=0;i<50;i++){
字符行号[100]=“”;
fgets(行号,100,RobotText);
sscanf(行号,“%d%s%d%f%f”,
&机器人信息[i].机器人编号,
机器人信息[i].机器人名称,
&RobotInfo[i].第二年制造商,
&机器人信息[i]。最高速度,
&机器人信息[i].麻省,
&机器人资讯[i].最佳成绩);
计数器++;
if(feof(ROBOTtxt)){
打破
}
}
if(ferror(RobotText)){
perror(“发生错误”);
出口(-1);
}
fclose(ROBOTtxt);
}
  • 这样编写的函数加载_Profiles()不接受任何参数,但在主函数中传递一个数组。您应该将声明重写为 “无效加载配置文件(Robot\t arr\u name[])”

  • 在main函数中,应仅通过参数名称传递参数。因此,与此相反: “正在加载_配置文件(Robot_t RobotInfo[]);” 只需通过: “正在加载_配置文件(RobotInfo);”


  • 您的代码中有许多错误,我将在下面粘贴我的注释解决方案,但仅供参考:

    • 加载_Profiles
      原型中,您没有指定参数
    • main
      中,您未正确调用函数
    • 加载_Profiles
      实现中,您试图处理本地数组,而不是作为参数传递的数组
    还有,有时候你用50,有时候5?是哪一个

    下面是使用Mockaroo上生成的文件测试的代码

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    #include <string.h>
    
    typedef struct
      {
        int Robot_Number;
        char Robot_Name[30];
        int Year_Manufacturer;
        float Top_Speed;
        float Mass;
        float Best_Score;
    } Robot_t;
    
    void loading_Profiles(Robot_t RobotInfo[]);  // added parameter to prototype
    
    int main()
    {
        Robot_t RobotInfo[50]; // 50 instead of 5
    
        loading_Profiles(RobotInfo); // simply pass your variable
    
        int i;
        for (i = 0; i < 50; i++) {
     
            printf("%d\t\t%s\t\t%d\t\t\t%.2f\t\t%.2f\t\t%.2f\n",
                    RobotInfo[i].Robot_Number, RobotInfo[i].Robot_Name,
                   RobotInfo[i].Year_Manufacturer, RobotInfo[i].Top_Speed, 
                   RobotInfo[i].Mass, RobotInfo[i].Best_Score);
     
        }
     
        return 0;
    }
    
    void loading_Profiles(Robot_t RobotInfo[]) // added parameter
    {
        int Counter = 0;
        int i;
    
        FILE *ROBOTtxt = fopen("Robot.txt", "r");
        
        //Robot_t RobotInfo[5]; // removed, work on the array passed as argument, not a local one
        
        if (ROBOTtxt == NULL) {
            perror("an error occured during the loading of the file\n");
            exit(-1);
        }
    
        for (i = 0; i < 50; i++) {
            char LineNumber[100] = "";
            fgets(LineNumber, 100, ROBOTtxt);
            sscanf(LineNumber, "%d %s %d %f %f %f",
                    &RobotInfo[i].Robot_Number,
                    RobotInfo[i].Robot_Name,
                    &RobotInfo[i].Year_Manufacturer,
                    &RobotInfo[i].Top_Speed,
                    &RobotInfo[i].Mass,
                    &RobotInfo[i].Best_Score);
    
            Counter++;
    
            if (feof(ROBOTtxt)) {
                break;
            }
        }
    
        if (ferror(ROBOTtxt)) {
            perror("an error has occured");
            exit(-1);
        }
    
        fclose(ROBOTtxt);
    }
    
    #包括
    #包括
    #包括
    #包括
    #包括
    类型定义结构
    {
    整数;
    char Robot_名称[30];
    国际汽车制造商;
    浮顶速度;
    浮体;
    浮动最佳分数;
    }机器人;
    无效加载配置文件(Robot_t RobotInfo[]);//为原型添加了参数
    int main()
    {
    Robot_t RobotInfo[50];//50而不是5
    加载_配置文件(RobotInfo);//只需传递变量
    int i;
    对于(i=0;i<50;i++){
    printf(“%d\t\t%s\t\t%d\t\t%.2f\t\t%.2f\t\t%.2f\t\t%.2f\n”,
    机器人信息[i]。机器人信息编号,机器人信息[i]。机器人名称,
    机器人信息[i]。年度制造商,机器人信息[i]。最高速度,
    机器人信息[i]。质量,机器人信息[i]。最佳成绩);
    }
    返回0;
    }
    无效加载配置文件(Robot\t RobotInfo[])//添加了参数
    {
    int计数器=0;
    int i;
    文件*ROBOTtxt=fopen(“Robot.txt”、“r”);
    //Robot_t RobotInfo[5];//已删除,处理作为参数传递的数组,而不是本地数组
    if(robotText==NULL){
    perror(“加载文件时出错\n”);
    出口(-1);
    }
    对于(i=0;i<50;i++){
    字符行号[100]=“”;
    fgets(行号,100,RobotText);
    sscanf(行号,“%d%s%d%f%f”,
    &机器人信息[i].机器人编号,
    机器人信息[i].机器人名称,
    &RobotInfo[i].第二年制造商,
    &机器人信息[i]。最高速度,
    &机器人信息[i].麻省,
    &机器人资讯[i].最佳成绩);
    计数器++;
    if(feof(ROBOTtxt)){
    打破
    }
    }
    if(ferror(RobotText)){
    perror(“发生错误”);
    出口(-1);
    }
    fclose(ROBOTtxt);
    }
    

    声明函数的参数时让我知道它是否有效,您需要将变量类型指定为结构并创建结构类型的数组

    void loading_Profiles(Robot_t RobotInfo[]) // added parameter
    

    不,对不起,那是我和我的朋友几个小时前的事,仍然没有解决方案。我想你把函数声明和函数调用搞混了<代码>Robot\u t RobotInfo[]应该出现在声明的括号内。它不应出现在函数体中。在通话中,您只需
    RobotInfo
    。我建议你重读C语言书中关于函数的章节。@Zac Nicholson-对另一个问题的评论正好解决了你现在的问题;您仍然没有将解决方案合并到代码中是一个声明,表示函数接受未指定类型的未指定数量的参数。这是过时的,不是一个原型说函数不带参数。这将是
    void loading_profile(void)它们不一样。是的,我道歉。