Yaklefak Yaklefak - 1 month ago 6x
C Question

C - argument is of type struct <anonymous>

I get the error for the two arguments of my function:

//incompatible type for argument of swapStruct
//expected Men100mParticipant but argument is of type struct <anonymous>

My code is like this:

int main(){

swapStruct(phaseToBeSorted->phase_result[j-1], phaseToBeSorted->phase_result[j]); //error


phaseToBeSorted is of type Men100mPhaseDetails that is defined as:

typedef struct Men100mPhaseDetails{
char* nameCurrentPhase;
int current_phase;
Men100mParticipant phase_result;
} Men100mPhaseDetails * Men100mPhaseDetails;

While pase_result is supposed to be an array of Men100mparticipant. The typedef is given as is and I can't change it.

This is the declaration of Men100mparticipant:

typedef struct {
char nameOfParticipant[MAX_LEN_NAME];
double* scores[4];
} Men100mparticipant, *Men100mParticipant;

and this the declaration of the function swapStruct:

static void swapStruct(Men100mParticipant a, Men100mParticipant b);

I don't understand what is the problem and I'll be glad to get some help in solving the problem.


You need to use:

swapStruct(&phaseToBeSorted->phase_result[j-1], &phaseToBeSorted->phase_result[j]);

The compiler is doing its best, but the structure type names are confusing.

It actually work but why is a '&' needed?

The type of phaseRoBeSorted->phase_result[j-1] is Men100mparticipant with two lower-case p's — so it is a structure. The swapStruct function takes two arguments of type Men100mParticipant (with an upper-case P), so it takes two pointers. The & passes the address of the array elements, of course, so that the function gets pointers, not copies of the structure.

This typedef (which appears to be given by the course instructors and can't be changed) is gruesome:

typedef struct { … } Men100mparticipant, *Men100mParticipant;

The case of the first letter P determines whether the type is a pointer or not, which is a disaster in the making. In general, Is it a good idea to typedef pointers? says "No". When there is a need (I'm not convinced this is an example of when it might be needed), make the difference clearer than the use of upper-case vs lower-case part way through the name.