我要疯了,结构体赋值~~~~~~~

god_sun 2009-12-03 09:00:41

#pragma pack(1)
typedef struct _ComInfo
{

char Gift_Count[5];
char Store_Count[4];
char List_Count[4];

struct _ComInfo()
{
memset (Gift_Count, 0, 5);
memset (Store_Count, 0, 4);
memset (List_Count, 0, 4);
}
}ComInfo;
#pragma pack()

int _tmain(int argc, _TCHAR* argv[])
{
char acTmp[25];
ComInfo coIf;

strcpy (coIf.Gift_Count, "00001");
strcpy (coIf.Store_Count, "0002");
strcpy (coIf.List_Count, "0003");

printf("after gift ==%s\n", coIf.Gift_Count);
printf("after store ==%s\n", coIf.Store_Count);
printf("after list ==%s\n", coIf.List_Count);
return 0;
}

//打印无问题,如果结构改成

typedef struct _ComInfo
{

char Gift_Count[5]; //赠送数量 小包装
char Order_Money[7]; //单比商品金额
char Store_Count[4]; //库存数
char List_Count[4]; //陈列数

struct _ComInfo()
{
memset (Gift_Count, 0, 5);
memset (Order_Money, 0, 7);
memset (Store_Count, 0, 4);
memset (List_Count, 0, 4);
}


strcpy (coIf.Gift_Count, "00001");
strcpy (coIf.Store_Count, "0002");
strcpy (coIf.List_Count, "0003");
strcpy (coIf.Order_Money, "0004000");

printf("after gift ==%s\n", coIf.Gift_Count);
printf("after store ==%s\n", coIf.Store_Count); //打印不出来
printf("after list ==%s\n", coIf.List_Count);
printf("after money ==%s\n", coIf.Order_Money);
}ComInfo;



...全文
151 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
tongxu1984 2009-12-03
  • 打赏
  • 举报
回复
程序没崩,算你运气好,基本都越界了。
z569362161 2009-12-03
  • 打赏
  • 举报
回复
88分是88的意思吗?

给我22分行吗?谢谢

你的ComInfo;
错误XXXXXXXXXXXXXXXXXXXXXXXXX
老邓 2009-12-03
  • 打赏
  • 举报
回复
你打印不出来,是因为在printf的时候,没有碰到字符串结束符,导致内存访问越界了。
所以程序崩溃了。
jackyjkchen 2009-12-03
  • 打赏
  • 举报
回复
第二种情况不清楚楼主是从何处抄来的……以前学的是什么语言?
老邓 2009-12-03
  • 打赏
  • 举报
回复
int main()
{
char c = '0';
printf("%d", c);
return 0;
}


输出48,而不是0!
老邓 2009-12-03
  • 打赏
  • 举报
回复
以char Gift_Count[5];为例,这个数组中只能放四个字符,比如“0000”
那第五个干什么用呢?
放字符串的结束符'\0'

注意:字符串中的0,(“0000”)不是字符串结束的0
jackyjkchen 2009-12-03
  • 打赏
  • 举报
回复
根本无法编译……
老邓 2009-12-03
  • 打赏
  • 举报
回复
typedef struct _ComInfo
{

char Gift_Count[5]; //赠送数量 小包装
char Order_Money[7]; //单比商品金额
char Store_Count[4]; //库存数
char List_Count[4]; //陈列数

struct _ComInfo()
{
memset (Gift_Count, 0, 5);
memset (Order_Money, 0, 7);
memset (Store_Count, 0, 4);
memset (List_Count, 0, 4);
}

}ComInfo;


using namespace std;

int main()
{
ComInfo coIf;
strcpy (coIf.Gift_Count, "0001");
strcpy (coIf.Store_Count, "002");
strcpy (coIf.List_Count, "003");
strcpy (coIf.Order_Money, "000400");

printf("after gift ==%s\n", coIf.Gift_Count);
printf("after store ==%s\n", coIf.Store_Count); //打印不出来
printf("after list ==%s\n", coIf.List_Count);
printf("after money ==%s\n", coIf.Order_Money);
return 0;
}
jackyjkchen 2009-12-03
  • 打赏
  • 举报
回复
结构体不是函数,是个数据结构,C++兼容时可以声明定义成员函数,但不能直接调用……
老邓 2009-12-03
  • 打赏
  • 举报
回复
strcpy (coIf.Gift_Count, "0001");
strcpy (coIf.Store_Count, "002");
strcpy (coIf.List_Count, "003");
strcpy (coIf.Order_Money, "004000");
老邓 2009-12-03
  • 打赏
  • 举报
回复
楼主啊,你要为每一个字符串留一个\0

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧