CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  C语言

这个程序的问题是怎么回事啊?

楼主zk2486(EMV)2004-12-01 20:27:09 在 C/C++ / C语言 提问

#include<stdio.h>  
  #include   "string.h"  
  //using   namespace   std;  
   
  #pragma   pack(8)  
  typedef   struct   a  
  {  
        double   b;  
        char   c;  
        int   i;  
        char   c1;  
  }   A,*PA   ;  
  #pragma   pack()  
   
  #pragma   pack(4)  
  typedef   struct   b  
  {  
        double   b;  
        char   c;  
        int   i;  
        char   c1;  
  }   B,*PB   ;  
  #pragma   pack()  
   
  #pragma   pack(2)  
  typedef   struct   c  
  {  
  double   b;  
  char   c;  
  int   i;  
  char   c1;  
  }   C,*PC   ;  
  #pragma   pack()  
   
  #pragma   pack(1)  
  typedef   struct   d  
  {  
  double   b;  
  char   c;  
  int   i;  
  char   c1;  
  }   D,*PD   ;  
  #pragma   pack()  
   
   
  int   main()  
  {  
  PA   pA   =   (PA)0;  
  PB   pB   =   (PB)0;  
  PC   pC   =   (PC)0;  
  PB   pD   =   (PD)0;  
  printf("sizeof(A)   =   %d\n",sizeof(A));  
  printf("A.b   offset   =   %d\n",&(pA->b));  
  printf("A.c   offset   =   %d\n",&(pA->c));  
  printf("A.i   offset   =   %d\n",&(pA->i));  
  printf("A.c1   offset   =   %d\n",&(pA->c1));  
  printf("\n");  
  printf("sizeof(B)   =   %d\n",sizeof(B));  
  printf("B.b   offset   =   %d\n",&(pB->b));  
  printf("B.c   offset   =   %d\n",&(pB->c));  
  printf("B.i   offset   =   %d\n",&(pB->i));  
  printf("B.c1   offset   =   %d\n",&(pB->c1));  
  printf("\n");  
  printf("sizeof(C)   =   %d\n",sizeof(C));  
  printf("C.b   offset   =   %d\n",&(pC->b));  
  printf("C.c   offset   =   %d\n",&(pC->c));  
  printf("C.i   offset   =   %d\n",&(pC->i));  
  printf("C.c1   offset   =   %d\n",&(pC->c1));  
  printf("\n");  
  printf("sizeof(A)   =   %d\n",sizeof(D));  
  printf("D.b   offset   =   %d\n",&(pD->b));  
  printf("D.c   offset   =   %d\n",&(pD->c));  
  printf("D.i   offset   =   %d\n",&(pD->i));  
  printf("D.c1   offset   =   %d\n",&(pD->c1));  
  printf("\n");  
   
  }  
   
  C:\Documents   and   Settings\zk\Cpp2.cpp(18)   :   error   C2380:   type(s)   preceding   'b'   (constructor   with   return   type,   or   illegal   redefinition   of   current   class-name?)  
  C:\Documents   and   Settings\zk\Cpp2.cpp(29)   :   error   C2380:   type(s)   preceding   'c'   (constructor   with   return   type,   or   illegal   redefinition   of   current   class-name?)  
  C:\Documents   and   Settings\zk\Cpp2.cpp(51)   :   error   C2440:   'initializing'   :   cannot   convert   from   'struct   d   *'   to   'struct   b   *'  
                  Types   pointed   to   are   unrelated;   conversion   requires   reinterpret_cast,   C-style   cast   or   function-style   cast  
  C:\Documents   and   Settings\zk\Cpp2.cpp(59)   :   error   C2273:   'function-style   cast'   :   illegal   as   right   side   of   '->'   operator  
  C:\Documents   and   Settings\zk\Cpp2.cpp(66)   :   error   C2273:   'function-style   cast'   :   illegal   as   right   side   of   '->'   operator  
  C:\Documents   and   Settings\zk\Cpp2.cpp(71)   :   error   C2273:   'function-style   cast'   :   illegal   as   right   side   of   '->'   operator  
  C:\Documents   and   Settings\zk\Cpp2.cpp(77)   :   warning   C4508:   'main'   :   function   should   return   a   value;   'void'   return   type   assumed  
  Error   executing   cl.exe. 问题点数:20、回复次数:2Top

1 楼fmddlmyy(寒潭)回复于 2004-12-01 20:39:43 得分 10

将程序保存成c文件,例如main.c  
   
  将  
  PB   pD   =   (PD)0;  
  改成  
  PD   pD   =   (PD)0;  
   
  最后加个  
  return   0;  
  就可以了。  
  Top

2 楼fmddlmyy(寒潭)回复于 2004-12-01 20:42:43 得分 10

如果保存成main.cpp,会有你列出的错误提示:  
   
  typedef   struct   b  
  {  
        double   b;  
        char   c;  
        int   i;  
        char   c1;  
  }   B,*PB   ;  
   
  这是因为结构名b和成员变量b重名。改成  
  #pragma   pack(4)  
  typedef   struct   sb  
  {  
        double   b;  
        char   c;  
        int   i;  
        char   c1;  
  }   B,*PB   ;  
  #pragma   pack()  
   
  #pragma   pack(2)  
  typedef   struct   sc  
  {  
  double   b;  
  char   c;  
  int   i;  
  char   c1;  
  }   C,*PC   ;  
  #pragma   pack()  
  即可。其它两个小问题同上。  
   
  Top

相关问题

  • 帮忙看这个程序是怎么回事
  • 这个是怎么回事?
  • 我的程序总是运行到这个函数出错, 怎么回事?
  • 有谁知道“windows\system\rpcss.exe”这个程序是怎么回事吗?
  • 大家看一下~这个程序是怎么回事!详情请进来看!
  • 求助!!各位大哥帮我看看这个小程序是怎么回事^_^
  • 这个lilo是怎么回事???
  • 这个是怎么回事?(win98)
  • 这个SQL语句是怎么回事?
  • 这个CSS是怎么回事?

关键词

  • pb
  • cpp
  • pc
  • pd
  • zk
  • pragma pack
  • offset
  • printf
  • operatorc
  • illegal

得分解答快速导航

  • 帖主:zk2486
  • fmddlmyy
  • fmddlmyy

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo