这个程序的问题是怎么回事啊?
#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




