首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

结构体,位赋值有关问题

2012-10-11 
结构体,位赋值问题C/C++ code#include iostreamusing namespace stdint main(){struct A{int a:1int b

结构体,位赋值问题

C/C++ code
#include <iostream>using namespace std;int main(){        struct A        {                int a:1;                int b:2;                int c:1;        }sa;        sa.a = 1;        sa.b = 3;        sa.c = 1;        printf("%d, %d, %d\n", sa.a, sa.b, sa.c);        return 0;}


为什么都输出-1呢?

[解决办法]
全部都是符号位为负,且都是1的补码,当然都是-1;不想如此的话,可以改为:
struct A
{
unsigned int a:1;
unsigned int b:2;
unsigned int c:1;
}sa;

热点排行