-1

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Yaklefak
  • 29
  • 6
  • This code doesn't compile. Provide a minimal and complete example. – Support Ukraine Aug 28 '16 at 10:46
  • 1
    What is `Men100mParticipant`? – alk Aug 28 '16 at 10:46
  • Also `phaseToBeSorted->phase_result[j-1]` and `phaseToBeSorted->phase_result` are not of the same type. And as `phaseToBeSorted->phase_result` is a `Men100mParticipant`, then `phaseToBeSorted->phase_result[j-1]` cannot be a `Men100mParticipant`. – alk Aug 28 '16 at 10:47
  • Also^2: Is this `typdef ... } Men100mPhaseDetails * Men100mPhaseDetails;` a typo, or what's the idea behind `... * Men100mPhaseDetails;`? – alk Aug 28 '16 at 10:51
  • Using this: `typedef struct { char nameOfParticipant[MAX_LEN_NAME]; double* scores[4]; } Men100mparticipant, *Men100mParticipant;`, where the case of the first letter P determines whether the type is a pointer or not, is a disaster in the making. In general, [Is it a good idea to typedef pointers](http://stackoverflow.com/questions/750178/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. – Jonathan Leffler Aug 28 '16 at 14:49
  • 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. – Jonathan Leffler Aug 28 '16 at 14:59
  • @JonathanLeffler Thank you very much, it actually work but why is a '&' needed ? I definitely agree with you and the whole exercise is about how working if pointers is horrible. – Yaklefak Aug 28 '16 at 15:10

1 Answers1

0

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.

Community
  • 1
  • 1
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278