1

I get "warning: assignment from incompatible pointer type" when compiling. How can I don't get that warning?

Here is some part of source code:

typedef struct
{
    char id[9];
    char fName[9];    
    char lName[9];
    int finalExam;
    int midTerm;
    float quiz1;
    float quiz2;
    float quiz3;
    float totalMark;
} Student;

....
....
....

pointAt = students; // initialize pointer
            float* topMark;
            char* topLname;
            char* topId;
            topMark = &(*pointAt).totalMark;
            topLname = &(*pointAt).lName;
            topId = &(*pointAt).id;
            printf("top guy : %s\n", topLname);
            pointAt += 1;

I get that warning at:

topLname = &(*pointAt).lName;
    topId = &(*pointAt).id;

these 2 lines causes that warning, because it points to char array. How can I fix this?

1 Answer 1

2

&(*pointAt).lName and &(*pointAt).id are of type char (*)[9], while the variables these values are being assigned to are of type char *, hence the mismatch.

Get rid of the & operator to get an expression of the char [9] which will decay to a char *. While you're at it, use the -> operator to reference the member of a struct pointer:

topLname = pointAt->lName;
topId = pointAt->id;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.