1. #include <stdio.h> 
  2. #include <stdlib.h> 
  3.  
  4. int main() 
  5.     struct {  
  6.         int a ; 
  7.         char b ; 
  8.         int c ; 
  9.     } a,*b; 
  10.     int d; 
  11.     char f; 
  12.     float g; 
  13.     double h; 
  14.  
  15.     printf("sizeof(a) = %d,sizeof(b) = %d ,sizeof(*b) = %d\n",sizeof(a),sizeof(b),sizeof(*b)); 
  16.     printf("sizeof(&d) = %d ,sizeof(&f) = %d ,sizeof(&g) = %d,sizeof(&h) = %d\n",sizeof(&d),sizeof(&f),sizeof(&g),sizeof(&h)); 
  17.     printf("sizeof(d) = %d ,sizeof(f) = %d ,sizeof(g) = %d,sizeof(h) = %d\n",sizeof(d),sizeof(f),sizeof(g),sizeof(h)); 

上面程序输出的结果为:

 
  
  1. sizeof(a) = 12,sizeof(b) = 8 ,sizeof(*b) = 12 
  2. sizeof(&d) = 8 ,sizeof(&f) = 8 ,sizeof(&g) = 8,sizeof(&h) = 8 
  3. sizeof(d) = 4 ,sizeof(f) = 1 ,sizeof(g) = 4,sizeof(h) = 8 

在系统中 sizeof( char *)、sizeof( int *)、sizeof( float *)、sizeof( double *) 的长度均为8

所以在sizeof时千万要注意,使用不当会出现错误;

 

我的使用的是系统和编译器为:

 
  
  1. [struggle@struggle alloc]$ cc -v  
  2. 使用内建 specs。  
  3. COLLECT_GCC=cc  
  4. COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/4.5.1/lto-wrapper  
  5. 目标:x86_64-redhat-linux  
  6. 配置为:../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-bootstrap --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --enable-languages=c,c++,objc,obj-c++,java,fortran,ada,lto --enable-plugin --enable-java-awt=gtk --disable-dssi --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --disable-libjava-multilib --with-ppl --with-cloog --with-tune=generic --with-arch_32=i686 --build=x86_64-redhat-linux  
  7. 线程模型:posix  
  8. gcc 版本 4.5.1 20100924 (Red Hat 4.5.1-4) (GCC)   
  9. [struggle@struggle alloc]$ uname -a 
  10. Linux struggle 2.6.35.13-91.fc14.x86_64 #1 SMP Tue May 3 13:23:06 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux 
  11. [struggle@struggle alloc]$