I have a function that returns a struct (not a struct pointer), but when trying to set the return value to a struct variable of the same type, I get “incompatible types.”
This is what the struct definition and function implementation look like:
typedef struct{
int ssn;
char FirstName[12];
char LastName[12];
int income;
} MyRecord;
MyRecord parseNextRecord()
{
MyRecord record;
// parse and initialize
return record;
}
And this is me calling it from within my main function:
MyRecord nextRecord;
nextRecord = parseNextRecord(); // "error: incompatible types in assignment"
Really stumped about this. Thanks in advance for your help.
Without seeing the header file, the probable cause is that
main()does not see a declaration of theparseNextRecord()function which results in the compiler generating an implicit declaration for it, with a return type ofint. This will cause the incompatible assignment error as it is not possible to assign anintto aMyRecord. To resolve, add declaration to the header file: