You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在C语言函数中传递结构体数组?求助解决‘expected expression before 'Robot_t'’编译错误

Fixing the 'expected expression before 'Robot_t'' Error & Other Issues in Your C Code

Hey there, I can totally relate to the frustration of staring at a compiler error for hours—let’s get this sorted out for you. The expected expression before 'Robot_t' error is just the tip of the iceberg, so we’ll fix that and a few other critical issues in your code step by step.

1. The Immediate Syntax Error (Causing the Compiler Message)

When you call a function in C, you don’t need to re-declare the type of the argument. Your line:

loading_Profiles (Robot_t RobotInfo[]);

is incorrect. You should just pass the array name directly, like this:

loading_Profiles(RobotInfo);

2. Mismatched Function Declaration & Definition

Your function is supposed to accept a Robot_t array, but your initial declaration:

void loading_Profiles();

doesn’t reflect that. Update the declaration to match what the function actually does:

void loading_Profiles(Robot_t RobotInfo[]);

You’ll also need to update the function definition to take this parameter (instead of declaring a local RobotInfo array inside the function—more on that next).

3. Local Array in loading_Profiles Doesn’t Update the Main Array

Right now, your loading_Profiles function creates its own local Robot_t RobotInfo[5]; array. Any data you read into this array stays inside the function and gets destroyed when the function ends—your main array never gets updated! Instead, use the array passed from main directly.

4. Array Size Mismatch

You declared Robot_t RobotInfo[5]; in main, but then loop 50 times trying to write to it. That’s a buffer overflow waiting to happen! If your Robot.txt has 50 entries, declare the array as Robot_t RobotInfo[50]; instead.

Fixed Full Code

Here’s the revised code with all these fixes applied:

#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;

// Updated function declaration to accept a Robot_t array
void loading_Profiles(Robot_t RobotInfo[]);

int main() {
    // Array size set to 50 to match the loop in loading_Profiles
    Robot_t RobotInfo[50];
    // Correct function call (no type declaration here)
    loading_Profiles(RobotInfo);
    
    int i;
    // Loop only up to the number of entries actually read, or 50 max
    for (i = 0; i < 50; i++) {
        // Added a check to avoid printing uninitialized entries if the file has less than 50
        if (RobotInfo[i].Robot_Number == 0 && RobotInfo[i].Year_Manufacturer == 0 && strlen(RobotInfo[i].Robot_Name) == 0) {
            break;
        }
        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;
}

// Updated function definition to use the passed array instead of a local one
void loading_Profiles(Robot_t RobotInfo[]) {
    int Counter = 0;
    int i;
    FILE *ROBOTtxt = fopen("Robot.txt", "r");
    if (ROBOTtxt == NULL) {
        perror("An error occurred during the loading of the file");
        exit(-1);
    }
    for (i = 0; i < 50; i++) {
        char LineNumber[100] = "";
        if (fgets(LineNumber, 100, ROBOTtxt) == NULL) {
            break; // Exit loop if we can't read a line (end of file or error)
        }
        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 (ferror(ROBOTtxt)) {
        perror("An error has occurred");
        exit(-1);
    }
    fclose(ROBOTtxt);
}

Additional Notes

  • I added a check in the main loop to stop printing once we hit uninitialized entries (in case your Robot.txt has fewer than 50 lines).
  • Replaced the feof check with checking the return value of fgets—this is a more reliable way to detect the end of a file in C.

内容的提问来源于stack exchange,提问作者Zac Nicholson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 18:22:32