C语言编译报错:expected declaration specifiers or ‘...’ before ‘Nanar_t’ 求助
编译错误及代码修正方案
核心错误原因
你当前的结构体定义只是创建了一个**struct Nanars类型的变量Nanar_t**,而非将Nanar_t定义为结构体的类型别名,因此编译器无法识别Nanar_t作为类型使用,直接触发了expected declaration specifiers or ‘...’ before ‘Nanar_t’错误。
分步修正
1. 修正结构体定义
使用typedef将结构体定义为类型别名,让Nanar_t成为合法的类型标识:
typedef struct Nanars{ char* nom; int duree; int cote; int idClient; } Nanar_t;
如果不想用typedef,函数参数需写成struct Nanars* nana[nb],但前者写法更简洁通用。
2. 修复函数内的其他问题
你的代码还存在多处编译/逻辑错误:
if(j!=i)中的i未定义,推测你想写的是j!=anana是Nanar_t*类型的数组,访问元素的duree需用nana[j]->duree,而非nana->duree[j]- 循环结束返回
j逻辑错误,应该返回存储目标索引的indice - 代码存在大括号不匹配问题,
else if块缺少闭合括号 - 可以用
abs()函数简化绝对值判断,避免重复写分支逻辑
修正后的完整代码
typedef struct Nanars{ char* nom; int duree; int cote; int idClient; } Nanar_t; int trouverDureeMin(int nb, int a, Nanar_t* nana[nb]){ int dureeMin = 100; int indice = 0; for(int j = 0; j < nb; j++){ if(j != a){ int diff = abs(nana[j]->duree - nana[a]->duree); if(diff < dureeMin){ indice = j; dureeMin = diff; } } } return indice; }
内容的提问来源于stack exchange,提问作者yougs dnt
相关产品推荐
相关产品推荐

