分配给char类型时

分配给char类型时

本文介绍了编译错误"分配给char类型时,不兼容的类型从类型为int [2]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有上述编译错误。 code的线路是这样的:

  IF((STRCMP(tempDept,数据[1] .Dept)== 0)及和放大器; tempCourse ==数据[I] .course){
            如果(tempDay =数据[I] .meet_days&放大器;&安培;
                tempTime ==数据[I] .start.hour){//< ---这行
                    的printf(这工作);
            }
        }

下面是我的结构声明:

 的typedef枚举{兆瓦,TR}天;typedef结构{
  INT小时,分钟;
} 时间;typedef结构{
  炭系[5];
  当然诠释,教派;
  天meet_days;
  时间开始,结束;
  原音乐的char [20];
} sched_record;

这是我的名单关闭变量:

  INT switchInput;
INT I = 0;
INT tempCourse = 0;
炭tempDept [5];
炭tempDay [2];
诠释tempTime;
// CHAR tempTime [1];
FILE *为FilePointer;
sched_record数据[MAX_RECORD]

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


解决方案

  tempDay =数据[I] .meet_days

这couses一个问题,因为 tempDay 是一个长度为2和 meet_days 是枚举天。而关于C中枚举常量都只是 INT 键入。你不能分配 INT 字符数组本身的另一个问题。也许你想要一个等号 ==
现在,你必须思考如何转换 INT 枚举值的char [2] 。一种方法是使用的sprintf()来实现这一目标。但具体的实现依赖于枚举常量的跨pretation。

I am having the above mentioned compile error. The line of code is this:

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");
            }
        }

Here is my structs declarations:

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;

And here is my list off variables:

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];

Can someone tell me how to fix this?

解决方案
tempDay = data[i].meet_days

This couses a problem because tempDay is char array of length 2 and meet_days is enum days. And in C constants in enums are just of int type. Another problem that you can't assign int to char array itself. Maybe you wanted an equal sign == ?Now you must think how to convert int enum value to char[2]. One way is use sprintf() to accomplish that. But concrete implementation depends on your interpretation of enum constants.

这篇关于编译错误&QUOT;分配给char类型时,不兼容的类型从类型为int [2]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:38