问题描述
我正在尝试使用结构 A 的大小
#define IOCTL_XYZ _IOWR(MAJOR_NUMBER,10,A)
用于定义 IOCTL 但我收到错误
error: case label does not reduce to an integer constant
case IOCTL_XYZ:
因为 IOCTL_XYZ 在 ioctl 处理程序中与 case 语句一起使用时不是整数常量。
#define ARRAY_SIZE1 4
#define ARRAY_SIZE2 4
#define ARRAY_SIZE3 8
#define ARRAY_SIZE4 4
#define ARRAY_SIZE5 6
typedef struct
{
union
{
struct
{
uint64_t item1 : 4;
uint64_t item2 : 4;
uint64_t item3 : 8;
uint64_t item4 : 48;
};
uint64_t combined;
};
union
{
uint64 array1[ARRAY_SIZE4];
uint64 array2[ARRAY_SIZE5];
uint64 array3[ARRAY_SIZE4];
};
} B;
typedef struct
{
uint64 element1;
uint64 element2[ARRAY_SIZE1];
B element3[ARRAY_SIZE2];
uint64 element4[ARRAY_SIZE3];
} A;
我有其他 ioctl 使用这样的结构,例如:
typedef struct B
{
uint32 item1;
uint32 item2;
} B;
typedef struct C
{
uint32 item1;
} C;
typedef struct
{
uint32 item1;
bool item2;
union
{
B element1;
C element2;
};
uint32 item3;
} A;
并且不会出现此错误。 是由于结构数组吗?但是数组有固定的大小吗?在这种情况下,编译器如何在编译时不知道大小?
解决方法
此错误的原因之一是数组大小对于 IOCTL 来说太大,这使总大小超过了允许的 16K 限制。 我在问题中编辑了此示例代码的数组大小,因此它可以正常工作。