对C语言中结构体的测试分析
2008-05-21 22:18
218 查看
今天看到一本书中有与下面这种结构体类似的使用形式:
typedef struct some_dev{
unsigned int var1 : 8;
unsigned int var2 : 13;
}
特意在VC++ 6.0中做了几个测试:
测试1:
#include <stdio.h>
typedef struct test1{
unsigned int testint:8;
char *p :32;
int test2 : 16;
} test;
int main()
{
test obj;
obj.testint = 32;
obj.test2 = 256;
char *t="hello world!";
obj.p=t;
printf("\n\t%u.......%d.........%s", obj.testint, obj.test2 , obj.p);
return 0;
}
编译结果:
struct_t.obj - 2 error(s), 0 warning(s)
error C2033: 'p' : bit field cannot have indirection
error C2034: 'p' : type of bit field too small for number of bits
即便指针p的长度为32或者64,编译都不能通过。
所以我们可以这样认为:
1:指针类型变量不能指定所占的位数
测试2:
#include <stdio.h>
typedef struct test1{
unsigned int testint:8;
char *p;
int test2 : 16;
} test;
int main()
{
test obj;
obj.testint = 32;
obj.test2 = 256;
char *t="hello world!";
obj.p=t;
printf("\n\t%u.......%d.........%s\n", obj.testint, obj.test2 , obj.p);
return 0;
}
编译链接均没有错误警告信息,运行结果为:
32.......256.........hello world!Press any key to continue
测试3:
#include <stdio.h>
typedef struct test1{
unsigned int testint:8;
char *p;
int test2 : 16;
} test;
int main()
{
test obj;
obj.testint = 256; //这里修改obj.testint的值为256
obj.test2 = 256;
char *t="hello world!";
obj.p=t;
printf("\n\t%u.......%d.........%s\n", obj.testint, obj.test2 , obj.p);
return 0;
}
运行结果为:
0.......256.........hello world!
可以看到,成员变量obj.testint的运行结果已经变成“0”.
为什么呢??
原来在结构体声明的时候,我们给obj.testint的bit数声明为8b的unsigned类型,它可以表示的最大值为(2^8 - 1),即255,我们在第三个测试中,给它赋值256,它的二进制值为:1 0000 0000 b,系统在保留时,只保留了后8位的值,对溢出的位,进行了截取抛弃处理,所以这里输出的结果为0.
而输出sizeof(struct test)的结果为:12
也就是说这三个成员变量各占4个字节。
再测试几组数据,结果也都能证明下面的结论:
2. 在声明成员变量时,可以用
变量名 :bit数;
来确定结构体类型的成员变量的值所占的字位数,如果在实际应用中,该变量的值超出了在声明它时所声明的字位数,那么溢出的部分将会丢失。
typedef struct some_dev{
unsigned int var1 : 8;
unsigned int var2 : 13;
}
特意在VC++ 6.0中做了几个测试:
测试1:
#include <stdio.h>
typedef struct test1{
unsigned int testint:8;
char *p :32;
int test2 : 16;
} test;
int main()
{
test obj;
obj.testint = 32;
obj.test2 = 256;
char *t="hello world!";
obj.p=t;
printf("\n\t%u.......%d.........%s", obj.testint, obj.test2 , obj.p);
return 0;
}
编译结果:
struct_t.obj - 2 error(s), 0 warning(s)
error C2033: 'p' : bit field cannot have indirection
error C2034: 'p' : type of bit field too small for number of bits
即便指针p的长度为32或者64,编译都不能通过。
所以我们可以这样认为:
1:指针类型变量不能指定所占的位数
测试2:
#include <stdio.h>
typedef struct test1{
unsigned int testint:8;
char *p;
int test2 : 16;
} test;
int main()
{
test obj;
obj.testint = 32;
obj.test2 = 256;
char *t="hello world!";
obj.p=t;
printf("\n\t%u.......%d.........%s\n", obj.testint, obj.test2 , obj.p);
return 0;
}
编译链接均没有错误警告信息,运行结果为:
32.......256.........hello world!Press any key to continue
测试3:
#include <stdio.h>
typedef struct test1{
unsigned int testint:8;
char *p;
int test2 : 16;
} test;
int main()
{
test obj;
obj.testint = 256; //这里修改obj.testint的值为256
obj.test2 = 256;
char *t="hello world!";
obj.p=t;
printf("\n\t%u.......%d.........%s\n", obj.testint, obj.test2 , obj.p);
return 0;
}
运行结果为:
0.......256.........hello world!
可以看到,成员变量obj.testint的运行结果已经变成“0”.
为什么呢??
原来在结构体声明的时候,我们给obj.testint的bit数声明为8b的unsigned类型,它可以表示的最大值为(2^8 - 1),即255,我们在第三个测试中,给它赋值256,它的二进制值为:1 0000 0000 b,系统在保留时,只保留了后8位的值,对溢出的位,进行了截取抛弃处理,所以这里输出的结果为0.
而输出sizeof(struct test)的结果为:12
也就是说这三个成员变量各占4个字节。
再测试几组数据,结果也都能证明下面的结论:
2. 在声明成员变量时,可以用
变量名 :bit数;
来确定结构体类型的成员变量的值所占的字位数,如果在实际应用中,该变量的值超出了在声明它时所声明的字位数,那么溢出的部分将会丢失。
相关文章推荐
- 对C语言中结构体的测试分析
- C语言结构体做参数易错情况分析
- 深入分析C语言中结构体指针的定义与引用详解
- 深入分析C语言中结构体指针的定义与引用详解
- [C专家]分析C语言声明—— 关于结构体
- 张孝祥的C语言测试题及讲解分析
- 【C语言】 C 语言 关键字分析 ( 属性关键字 | 常量关键字 | 结构体关键字 | 联合体关键字 | 枚举关键字 | 命名关键字 | 杂项关键字)
- c语言中结构体占用内存分析
- C语言返回值为结构体的汇编分析
- 测试一下C语言结构体初始化
- C语言结构体传参分析
- C语言全局变量,局部变量,结构体分析,枚举类型笔记
- C语言内存中结构体对齐分析
- C语言下的结构体间赋值的深度分析
- C语言 结构体传值与传址分析
- 绝对能够测试你的C语言功力的几个问题及其讲解分析
- C专家编程-Chapter3 C语言声明分析2
- 【性能】性能测试数据分析
- 黑马程序员--C语言中的结构体
- Mysql 性能测试分析与优化