Expected expression before 't_type' error in C code

Hello,
I met this problem to compile the code, which gave me the error message as:

kmer_SuffixArray01.c:19:28: 
        error: expected expression before 't_rotation'

Here is my code fragment:

//kmer_SuffixArray01.c
//Structure to store the data of a rotation
typedef struct rotation{
    char* suffix;
    int index;
} t_rotation;

//comparator for qsort ()  function
int cmpFunc(const void* x, const void* y) {
    t_rotation* ptr1 = t_rotation* x;    //Line 19: Must be casted???
    t_rotation* ptr2 = (t_rotation*) y;  //Line 20 correct way!

    return strcmp(ptr1->suffix, ptr2->suffix);
}
......

I was wondering the error might be a simple problem about the syntax to cast/bracket const void pointer (const void x)* variable in C syntax.
The following Line 20 is fine which has brackets as explicit cast (?) from void to specific (t_rotation). I do not know how to specify the problem to google.
Why the bracket is needed here? Thanks a lot!

My two penneth worth:
To cast const void* x to a pointer of type t_rotation , parenthesis (operator precedence forcing the 'cast') are required otherwise it looks like your are trying to assign ptr1 some sort of weird multiplication of t_rotation * x ... which, obviously makes no sense.

3 Likes

I did not think about the possible "multiplication" of the syntax. I like the overall simple grammar of C, but those exceptions (especially those pointer-related) makes C difficult. Thanks for the clarification!

1 Like

never confuse 'simple' for 'easy' :smiley: (I've done it more times than I care to recall :thinking:)

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.