【发布时间】:2016-08-28 10:40:49
【问题描述】:
我的函数的两个参数出现错误:
//incompatible type for argument of swapStruct
//expected Men100mParticipant but argument is of type struct <anonymous>
我的代码是这样的:
int main(){
...
swapStruct(phaseToBeSorted->phase_result[j-1], phaseToBeSorted->phase_result[j]); //error
...
}
phaseToBeSorted 是 Men100mPhaseDetails 类型,定义为:
typedef struct Men100mPhaseDetails{
char* nameCurrentPhase;
int current_phase;
Men100mParticipant phase_result;
} Men100mPhaseDetails * Men100mPhaseDetails;
虽然 pase_result 应该是 Men100mparticipant 的数组。 typedef 按原样给出,我无法更改。
这是 Men100mparticipant 的声明:
typedef struct {
char nameOfParticipant[MAX_LEN_NAME];
double* scores[4];
} Men100mparticipant, *Men100mParticipant;
这是函数swapStruct的声明:
static void swapStruct(Men100mParticipant a, Men100mParticipant b);
我不明白问题出在哪里,我很乐意在解决问题方面得到一些帮助。
【问题讨论】:
-
此代码无法编译。提供一个最小且完整的示例。
-
什么是
Men100mParticipant? -
另外
phaseToBeSorted->phase_result[j-1]和phaseToBeSorted->phase_result不是同一类型。因为phaseToBeSorted->phase_result是Men100mParticipant,所以phaseToBeSorted->phase_result[j-1]不能是Men100mParticipant。 -
Also^2:这是
typdef ... } Men100mPhaseDetails * Men100mPhaseDetails;的错字吗,或者... * Men100mPhaseDetails;背后的想法是什么? -
使用这个:
typedef struct { char nameOfParticipant[MAX_LEN_NAME]; double* scores[4]; } Men100mparticipant, *Men100mParticipant;,其中第一个字母 P 的大小写决定了类型是否为指针,这是一场灾难。一般来说,Is it a good idea to typedef pointers 会说“不”。当有需要时(我不相信这是一个可能需要它的例子),让区别比在名称中使用大写和小写部分更清楚。