从类型int分配类型char 时编译“不兼容类型”的错误

我有上面提到的编译错误。 代码行是这样的:

if ((strcmp(tempDept, data[1].Dept)==0) && tempCourse == data[i].course){ if (tempDay = data[i].meet_days && tempTime == data[i].start.hour){ //<---This line printf("this worked"); } } 

这是我的结构声明:

 typedef enum {MW, TR} days; typedef struct { int hour, min; } Time; typedef struct { char Dept[5]; int course, sect; days meet_days; Time start, end; char instr[20]; } sched_record; 

这是我的变量列表:

 int switchInput; int i = 0; int tempCourse = 0; char tempDept[5]; char tempDay[2]; int tempTime; //char tempTime[1]; FILE *filePointer; sched_record data[MAX_RECORD]; 

有人能告诉我如何解决这个问题吗?

 tempDay = data[i].meet_days 

这会产生问题,因为tempDay是长度为2的char数组, meet_days是枚举days 。 在枚举中的C常量只是int类型。 另一个问题是你无法将int赋给char array本身。 也许你想要一个等号== ? 现在你必须考虑如何将int枚举值转换为char[2] 。 一种方法是使用sprintf()来实现这一点。 但具体的实现取决于你对枚举常量的解释。

 if (tempDay = data[i].meet_days 

你错过了双等号==