我正在尝试为结构中的位字段赋值。这是降级,因为指定的变量的尺寸小于右侧使用的类型。有没有办法强制进行类型转换,了解降级存在一些风险。这是测试代码,我不认为右手值会比我的位字段更大。
使用编译:
gcc -Wconversion compileError.c
代码:
#include "stdio.h"
typedef unsigned long int uint64_t;
typedef unsigned short int uint16_t;
typedef struct
{
uint64_t val:48;
uint16_t length;
}data_t;
static data_t testData[10] = {};
int main()
{
data_t* pData = (data_t*)&testData;
uint64_t var = 0;
pData->val = var + 1;
printf("Just trying to compile this program\n");
return 0;
}
编译器输出:
compileError.c: In function main:
compileError.c:19:22: warning: conversion to long unsigned
int:48 from uint64_t may alter its value [-Wconversion]
pData->val = var + 1;