C语言 - 结构体(struct)比特字段(:) 详细解释
2015-07-18 20:39
423 查看
结构体(struct)比特字段(:) 详细解释
本文地址: http://blog.csdn.net/caroline_wendy/article/details/26722511结构体(struct)能够使用位字段(:), 节省空间, 例如以下面代码,
结构体a中的, 第一个变量x占用1个字符, y占用2个字符, z占用33个字符(越界);
可是sizeof()会自己主动补齐, 如x+y一共占用4个字节, z占用8个字节, 所以结构体占用12个字节;
当使用加法运算时, 会初始化为0;
代码:
/* * test.cpp * * Created on: 2014.05.23 * Author: Spike */ /*eclipse cdt, gcc 4.8.1*/ #include <iostream> #include <stdio.h> using namespace std; struct a { int x:1; int y:2; int z:33; }; int main() { a d; cout << &d << std::endl; d.z = d.x + d.y; printf("%d %d %d %d\n", d.x, d.y, d.z, sizeof(d)); return 0; }
输出:
0x22fed4 0 0 0 12
相关文章推荐
- [C++ primer]虚函数和纯虚函数
- Leetcode-Implement Queue using Stacks(C++)
- C语言结构体、C++结构体和C++类的解析
- Leetcode-Implement Stack using Queues(C++)
- C++ 合并/去除字符串中多余的空格(创建新串)
- Leetcode-Min Stack(C++)
- LeetCode-Implement Trie (Prefix Tree) (C++)
- enum hack用法
- 实现一个函数,把一个字符串中的字符从小写转为大写。
- 栈变量被覆盖的问题在不同编译器中的表现,蛋疼的VC++
- c++ thread 笔记1
- C++学习笔记——面向对象
- C++中修改类的私有属性的方法
- VC++6.0 修改项目工程名称
- c++题目整理
- c++ primer读书笔记-第十五章 面向对象编程
- C++ 虚基类 派生与继承
- 黑马程序员-C语言基础知识-预处理
- 编写程序输出每种内置类型的长度
- C++中的链式操作