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

结构体填充函数重复声明参数类型不匹配错误排查求助

问题:fill_struct函数编译报错——多次声明且参数类型不一致

编写了用于填充结构体的fill_struct函数,但编译器提示该函数被多次声明且参数类型不一致,无法定位问题。相关代码如下:

struct juegos_interano {
    char participantes[60][15];
    char sexo[60];
    int medallas[10][4];
    char deportes_partisipados[60][10];
    char encargado[20];
    int max_persona;
};

struct todos_los_anos {
    struct  juegos_interano primer_ano;
    struct  juegos_interano segundo_ano;
    struct  juegos_interano tercer_ano;
    struct  juegos_interano cuarto_ano;
};

struct todos_los_anos create_random_table()
{

    srand(time(NULL));
    
    struct todos_los_anos all_year;
    
    //200 names
    char names[200][20] = {
        "Juan", "Pedro", "Luis" };
    //Sex in relation to names 
    
    char sex[200] = {
        'M', 'M', 'M' };
    
    char sports[10] = {'n','v','b','t','f','y','k','d','a','p'};
    
    int medals[40] = {1};
    int medallas = 40;
    
    int rand_total_name = 0;
    int rand_total_medallas = 0;
    int rand_name = 0;
    int rand_sport = 0;
    int rand_medals = 0;
    int random = 0;
    int r_name = 0;
    int i = 0;
    
    rand_total_name = rand() % 60 +20;
    
    rand_total_medallas = rand() % medallas;
    medallas = medallas - rand_total_medallas;
    
    rand_name = 0;
    rand_sport = 0;
    rand_medals = 0;
    random = 0;
    i = 0;
    
    for (i=0; i < rand_total_name; i++)
        rand_name = rand() % 200;
        rand_sport = rand() % 10;
        random = rand() % rand_sport;
        rand_medals = rand_medals - random;
        if (rand_medals<0)
            random = random + rand_medals; 
        fill_struct (&all_year.primer_ano, rand_total_name, rand_name, rand_sport, random, rand_total_medallas, i, names, sex, sports, medals);
    
    r_name = rand() % 200;
    strcpy(all_year.primer_ano.encargado, names[r_name]);
    
    rand_total_name = rand() % 60 +20;
    
    rand_total_medallas = rand() % medallas;
    medallas = medallas - rand_total_medallas;
    
    rand_name = 0;
    rand_sport = 0;
    rand_medals = 0;
    random = 0;
    i = 0;
    
    for (i=0; i < rand_total_name; i++)
        rand_name = rand() % 200;
        rand_sport = rand() % 10;
        random = rand() % rand_sport;
        rand_medals = rand_medals - random;
        if (rand_medals<0)
            random = random + rand_medals; 
        fill_struct (&all_year.segundo_ano, rand_total_name, rand_name, rand_sport, random, rand_total_medallas, i, names, sex, sports, medals);
    
    r_name = rand() % 200;
    strcpy(all_year.segundo_ano.encargado, names[r_name]);
    
    rand_total_name = rand() % 60 +20;
    
    rand_total_medallas = rand() % medallas;
    medallas = medallas - rand_total_medallas;
    // inicializaciones en 0 
    rand_name = 0;
    rand_sport = 0;
    rand_medals = 0;
    random = 0;
    i = 0;
    
    for (i=0; i < rand_total_name; i++)
        rand_name = rand() % 200;
        rand_sport = rand() % 10;
        random = rand() % rand_sport;
        rand_medals = rand_medals - random;
        if (rand_medals<0)
            random = random + rand_medals; 
        fill_struct (&all_year.tercer_ano, rand_total_name, rand_name, rand_sport, random, rand_total_medallas, i, names, sex, sports, medals);
    
    r_name = rand() % 200;
    strcpy(all_year.tercer_ano.encargado, names[r_name]);
    
    rand_total_name = rand() % 60 +20;
    
    rand_total_medallas = rand() % medallas;
    medallas = medallas - rand_total_medallas;
    
    rand_name = 0;
    rand_sport = 0;
    rand_medals = 0;
    random = 0;
    i = 0;
    
    for (i=0; i < rand_total_name; i++)
        rand_name = rand() % 200;
        rand_sport = rand() % 10;
        random = rand() % rand_sport;
        rand_medals = rand_medals - random;
        if (rand_medals<0)
            random = random + rand_medals; 
        fill_struct (&all_year.cuarto_ano, rand_total_name, rand_name, rand_sport, random, rand_total_medallas, i, names, sex, sports, medals);
    //llenar encargado
    r_name = rand() % 200;
    strcpy(all_year.cuarto_ano.encargado, names[r_name]);
    
    return all_year;

}

void fill_struct(struct juegos_interano *year, int rand_total_name, int rand_numb, int rand_sport, int rand_medals, int rand_total_medallas, int count, char names[200][20], char sex[200], char sports[10], int medals[40])
{
    int i = 0;
    int random = 0;

    strcpy(year->participantes[count], names[rand_numb]);
    year->sexo[count] = sex[rand_numb];
    year->max_persona = rand_total_name;
    
    
    for (i=0; i < rand_sport; i++){
        random = rand() % 10;
        year->deportes_partisipados[count][i] = sports[random];
    }
    
    for (i=0; i< rand_medals; i++){
        do{
            random = rand() % rand_total_medallas;
            if (medals[random]==1){
                if (medals[random]<10){
                    year->medallas[count][i] = 1;
                    medals[random] = 0;        
                }
                else if (medals[random]>=10 && medals[random]<20){
                    year->medallas[count][i] = 2;
                    medals[random] = 0;
                }
                else if (medals[random]>=20 && medals[random]<30){
                    year->medallas[count][i] = 3;
                    medals[random] = 0;
                }
                else if (medals[random]>=30){
                    year->medallas[count][i] = 4;
                    medals[random] = 0;
                }
            }
        }
        while(medals[random]==0);
    }

}

问题原因及修复方案

1. 核心错误:函数隐式声明导致的类型不匹配

在create_random_table函数中调用fill_struct时,编译器还未读取到fill_struct的定义,会自动隐式声明该函数——隐式声明的函数默认返回int类型,且参数列表与实际定义完全不符。当编译器后续看到fill_struct的实际void类型定义时,就会抛出"多次声明且参数类型不一致"的错误。

修复方法:

  • 方法一:在create_random_table函数之前,添加fill_struct的函数原型声明:
void fill_struct(struct juegos_interano *year, int rand_total_name, int rand_numb, int rand_sport, int rand_medals, int rand_total_medallas, int count, char names[200][20], char sex[200], char sports[10], int medals[40]);
  • 方法二:将fill_struct函数的完整定义移到create_random_table函数之前,让编译器先识别到函数的正确类型和参数。

2. 附带逻辑错误修复

代码中还有两个会导致运行异常的问题,建议一并修复:

  • for循环缺少大括号:所有for循环都未用{}包裹循环体,导致只有第一行代码属于循环,后续代码仅执行一次,完全破坏逻辑。例如:
// 错误写法
for (i=0; i < rand_total_name; i++)
    rand_name = rand() % 200;
    rand_sport = rand() % 10; // 这行不在循环内

需改为:

// 正确写法
for (i=0; i < rand_total_name; i++) {
    rand_name = rand() % 200;
    rand_sport = rand() % 10;
    random = rand() % rand_sport;
    rand_medals = rand_medals - random;
    if (rand_medals<0)
        random = random + rand_medals; 
    fill_struct (&all_year.primer_ano, rand_total_name, rand_name, rand_sport, random, rand_total_medallas, i, names, sex, sports, medals);
}
  • 除以0风险:rand_sport = rand() % 10可能生成0,此时random = rand() % rand_sport会触发除以0的运行时错误,建议添加判断:
rand_sport = rand() % 10;
if (rand_sport == 0) rand_sport = 1; // 避免除以0
random = rand() % rand_sport;

内容的提问来源于stack exchange,提问作者Danny Fiallo

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 22:14:54