user-0 提问: 如何查看位域成员的地址

回复:
user: user-0
content: 引用 10 楼 zhao4zhong1 的回复:仅供参考#include <stdio.h> #pragma pack(push,1) union U {     unsigned char byte;     struct BF {         unsigned int b0:1;//a         unsigned int b1:1;//b         unsigned int b2:1;//c     } bf; } u; #pragma pack(pop) unsigned char bt; int a,b,c; int main() {     for (bt=0;bt<8;bt++) {         u.byte=(unsigned char)bt;         a=u.bf.b0;         b=u.bf.b1;         c=u.bf.b2;         printf("byte 0x%02x -- c:%d b:%d a:%d\n",bt,c,b,a);     }     for (c=0;c<2;c++)     for (b=0;b<2;b++)     for (a=0;a<2;a++) {         u.bf.b0=a;         u.bf.b1=b;         u.bf.b2=c;         bt=u.byte;         printf("c:%d b:%d a:%d -- byte 0x%02x\n",c,b,a,bt);     }     return 0; } //byte 0x00 -- c:0 b:0 a:0 //byte 0x01 -- c:0 b:0 a:1 //byte 0x02 -- c:0 b:1 a:0 //byte 0x03 -- c:0 b:1 a:1 //byte 0x04 -- c:1 b:0 a:0 //byte 0x05 -- c:1 b:0 a:1 //byte 0x06 -- c:1 b:1 a:0 //byte 0x07 -- c:1 b:1 a:1 //c:0 b:0 a:0 -- byte 0x00 //c:0 b:0 a:1 -- byte 0x01 //c:0 b:1 a:0 -- byte 0x02 //c:0 b:1 a:1 -- byte 0x03 //c:1 b:0 a:0 -- byte 0x04 //c:1 b:0 a:1 -- byte 0x05 //c:1 b:1 a:0 -- byte 0x06 //c:1 b:1 a:1 -- byte 0x07   #include <stdio.h> #include <stdlib.h> #include <string.h> int main() {     float f;     double d;     char bs[65];     char b[65];     char s[80];     unsigned char *p;     char e[12];     char *t;     int ex;      while (1) {         printf("Input a float point number:");fflush(stdout);         rewind(stdin);         fgets(s,80,stdin);         if (1==sscanf(s,"%f",&f) && 1==sscanf(s,"%lf",&d)) break;     }     printf("f=%g\n",f);     p=(unsigned char *)&f;     printf("hex=%02X %02X %02X %02X\n",p[3],p[2],p[1],p[0]);     ltoa(*(long *)&f,b,2);     sprintf(bs,"%032s",b);     printf("bin=%s\n",bs);     printf("bin=%.1s %.8s   %s\n",bs,bs+1,bs+9);     strncpy(e,bs+1,8);e[8]=0;     ex=strtol(e,&t,2);     printf("    %c %-4d-127 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+9);     ex-=127;     printf("    %c %-8d 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+9);      printf("\nd=%lg\n",d);     p=(unsigned char *)&d;     printf("hex=%02X %02X %02X %02X %02X %02X %02X %02X\n",p[7],p[6],p[5],p[4],p[3],p[2],p[1],p[0]);     _i64toa(*(__int64 *)&d,b,2);     sprintf(bs,"%064s",b);     printf("bin=%s\n",bs);     printf("bin=%.1s %.11s   %s\n",bs,bs+1,bs+12);     strncpy(e,bs+1,11);e[11]=0;     ex=strtol(e,&t,2);     printf("    %c %-6d-1023 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+12);     ex-=1023;     printf("    %c %-11d 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+12);      return 0; } //Input a float point number:0.125 //f=0.125 //hex=3E 00 00 00 //bin=00111110000000000000000000000000 //bin=0 01111100   00000000000000000000000 //    + 124 -127 1.00000000000000000000000 //    + -3       1.00000000000000000000000 // //d=0.125 //hex=3F C0 00 00 00 00 00 00 //bin=0011111111000000000000000000000000000000000000000000000000000000 //bin=0 01111111100   0000000000000000000000000000000000000000000000000000 //    + 1020  -1023 1.0000000000000000000000000000000000000000000000000000 //    + -3          1.0000000000000000000000000000000000000000000000000000 //  多谢 多谢 例子很好

user: user-1
content: 引用 9 楼 yang382197207 的回复:[quote=引用 6 楼 richyhuang 的回复:] bit field是一种压缩的数据类型,其元素不一定以8为倍数的,所以只能看到整个结构的地址,而不能指定某一个元素的地址 可事实上有时候会想看一下，位域的排列，对齐，数据是都压缩存储，这个可以通过什么来查看？内存吗[/quote] 用watch试试吧,

user: user-2
content: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <locale.h> int main() {     int i,v;     char bs[33];     char b[33];     char hs[9];     char h[9];     char s[4];     char *e;  // 十进制整数转二进制串；     i=1024;     ltoa(i,b,2);     sprintf(bs,"%032s",b);     printf("i=%d,bs=%s\n",i,bs); // 十进制整数转十六进制串；     i=1024;     ltoa(i,h,16);     sprintf(hs,"%08s",h);     printf("i=%d,hs=%s\n",i,hs); // 十六进制字符串转成十进制数     strcpy(hs,"00000400");     sscanf(hs,"%x",&i);     printf("hs=%s,i=%d\n",hs,i); // 二进制字符串转化为十六进制字符串；     strcpy(bs,"00000000000000000000010000000000");     i=strtol(bs,&e,2);     ltoa(i,h,16);     sprintf(hs,"%08s",h);     printf("bs=%s,hs=%s\n",bs,hs); // 二进制字符串转化为十进制数；     strcpy(bs,"00000000000000000000010000000000");     i=strtol(bs,&e,2);     printf("bs=%s,i=%d\n",bs,i); // 十六进制字符串转成二进制串     strcpy(hs,"00000400");     sscanf(hs,"%x",&i);     ltoa(i,b,2);     sprintf(bs,"%032s",b);     printf("hs=%s,bs=%s\n",hs,bs); // ASC\GBK字符串转十六进制串     strcpy(s,"a汉");     i=0;     while (1) {         if (0==s[i]) break;         sprintf(hs+i*2,"%02X",(unsigned char)s[i]);         i++;     }     setlocale(LC_ALL,"chs");     printf("s=%s,hs=%s\n",s,hs); // 十六进制字符串转成汉字（GBK）及字符(ASC)     strcpy(hs,"61BABA");     i=0;     while (1) {         if (1!=sscanf(hs+i*2,"%2x",&v)) break;         s[i]=(char)v;         i++;     }     s[i]=0;     printf("hs=%s,s=%s\n",hs,s);      return 0;  } //i=1024,bs=00000000000000000000010000000000 //i=1024,hs=00000400 //hs=00000400,i=1024 //bs=00000000000000000000010000000000,hs=00000400 //bs=00000000000000000000010000000000,i=1024 //hs=00000400,bs=00000000000000000000010000000000 //s=a汉,hs=61BABA //hs=61BABA,s=a汉

user: user-2
content: 仅供参考#include <stdio.h> #pragma pack(push,1) union U {     unsigned char byte;     struct BF {         unsigned int b0:1;//a         unsigned int b1:1;//b         unsigned int b2:1;//c     } bf; } u; #pragma pack(pop) unsigned char bt; int a,b,c; int main() {     for (bt=0;bt<8;bt++) {         u.byte=(unsigned char)bt;         a=u.bf.b0;         b=u.bf.b1;         c=u.bf.b2;         printf("byte 0x%02x -- c:%d b:%d a:%d\n",bt,c,b,a);     }     for (c=0;c<2;c++)     for (b=0;b<2;b++)     for (a=0;a<2;a++) {         u.bf.b0=a;         u.bf.b1=b;         u.bf.b2=c;         bt=u.byte;         printf("c:%d b:%d a:%d -- byte 0x%02x\n",c,b,a,bt);     }     return 0; } //byte 0x00 -- c:0 b:0 a:0 //byte 0x01 -- c:0 b:0 a:1 //byte 0x02 -- c:0 b:1 a:0 //byte 0x03 -- c:0 b:1 a:1 //byte 0x04 -- c:1 b:0 a:0 //byte 0x05 -- c:1 b:0 a:1 //byte 0x06 -- c:1 b:1 a:0 //byte 0x07 -- c:1 b:1 a:1 //c:0 b:0 a:0 -- byte 0x00 //c:0 b:0 a:1 -- byte 0x01 //c:0 b:1 a:0 -- byte 0x02 //c:0 b:1 a:1 -- byte 0x03 //c:1 b:0 a:0 -- byte 0x04 //c:1 b:0 a:1 -- byte 0x05 //c:1 b:1 a:0 -- byte 0x06 //c:1 b:1 a:1 -- byte 0x07   #include <stdio.h> #include <stdlib.h> #include <string.h> int main() {     float f;     double d;     char bs[65];     char b[65];     char s[80];     unsigned char *p;     char e[12];     char *t;     int ex;      while (1) {         printf("Input a float point number:");fflush(stdout);         rewind(stdin);         fgets(s,80,stdin);         if (1==sscanf(s,"%f",&f) && 1==sscanf(s,"%lf",&d)) break;     }     printf("f=%g\n",f);     p=(unsigned char *)&f;     printf("hex=%02X %02X %02X %02X\n",p[3],p[2],p[1],p[0]);     ltoa(*(long *)&f,b,2);     sprintf(bs,"%032s",b);     printf("bin=%s\n",bs);     printf("bin=%.1s %.8s   %s\n",bs,bs+1,bs+9);     strncpy(e,bs+1,8);e[8]=0;     ex=strtol(e,&t,2);     printf("    %c %-4d-127 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+9);     ex-=127;     printf("    %c %-8d 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+9);      printf("\nd=%lg\n",d);     p=(unsigned char *)&d;     printf("hex=%02X %02X %02X %02X %02X %02X %02X %02X\n",p[7],p[6],p[5],p[4],p[3],p[2],p[1],p[0]);     _i64toa(*(__int64 *)&d,b,2);     sprintf(bs,"%064s",b);     printf("bin=%s\n",bs);     printf("bin=%.1s %.11s   %s\n",bs,bs+1,bs+12);     strncpy(e,bs+1,11);e[11]=0;     ex=strtol(e,&t,2);     printf("    %c %-6d-1023 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+12);     ex-=1023;     printf("    %c %-11d 1.%s\n",(bs[0]=='0')?'+':'-',ex,bs+12);      return 0; } //Input a float point number:0.125 //f=0.125 //hex=3E 00 00 00 //bin=00111110000000000000000000000000 //bin=0 01111100   00000000000000000000000 //    + 124 -127 1.00000000000000000000000 //    + -3       1.00000000000000000000000 // //d=0.125 //hex=3F C0 00 00 00 00 00 00 //bin=0011111111000000000000000000000000000000000000000000000000000000 //bin=0 01111111100   0000000000000000000000000000000000000000000000000000 //    + 1020  -1023 1.0000000000000000000000000000000000000000000000000000 //    + -3          1.0000000000000000000000000000000000000000000000000000 //

user: user-0
content: 引用 6 楼 richyhuang 的回复:bit field是一种压缩的数据类型,其元素不一定以8为倍数的,所以只能看到整个结构的地址,而不能指定某一个元素的地址 可事实上有时候会想看一下，位域的排列，对齐，数据是都压缩存储，这个可以通过什么来查看？内存吗

user: user-0
content: 引用 4 楼 lm_whales 的回复:位域无地址，但是可以看到位域变量占了哪些位； 方法是其他位为0，某位域变量全1 然后逐个字节，或者逐位输出。 这个方法不错，有用

user: user-0
content: 位域不能取地址的&

user: user-3
content: 位域无地址，但是可以看到位域变量占了哪些位； 方法是其他位为0，某位域变量全1 然后逐个字节，或者逐位输出。

user: user-3
content: 把他当成char 数组解释每个位即可

user: user-1
content: bit field是一种压缩的数据类型,其元素不一定以8为倍数的,所以只能看到整个结构的地址,而不能指定某一个元素的地址

user: user-4
content: 引用 1 楼 user-5 的回复: struct data  {        unsigned int  num1;        unsigned int num2 ; 	   char ch1;        unsigned int  num3;        unsigned int num4;  };  data a; unsigned int *p_nNum1=&a.num1; printf("%d ",p_nNum1,);   你写的这个都不是bit field。bit field不能取地址

user: user-5
content: 引用 1 楼 user-5 的回复: struct data  {        unsigned int  num1;        unsigned int num2 ; 	   char ch1;        unsigned int  num3;        unsigned int num4;  };  data a; unsigned int *p_nNum1=&a.num1; printf("%d ",p_nNum1,);    这个是偏移量 参考下  [code=c][/ #include "stdafx.h" #include<stdio.h>  struct data  {        unsigned int  num1;        unsigned int num2 ; 	   char ch1;        unsigned int  num3;        unsigned int num4;  };  int main(int argc, char* argv[]) { 	data a; 	unsigned int data::*p_nNum1=&data::num1; 	unsigned int data::*p_nNum2=&data::num2; 	unsigned int data::*p_nNum3=&data::num3; 	unsigned int data::*p_nNum4=&data::num4; 	char data::*p_cCh1=&data::ch1;   	printf("%d %d %d %d %d",p_nNum1,p_nNum2,p_nNum3,p_nNum4,p_cCh1);   	return 0; }code]

user: user-5
content: struct data  {        unsigned int  num1;        unsigned int num2 ; 	   char ch1;        unsigned int  num3;        unsigned int num4;  };  data a; unsigned int *p_nNum1=&a.num1; printf("%d ",p_nNum1,);