Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1032307
  • 博文数量: 264
  • 博客积分: 6005
  • 博客等级: 大校
  • 技术积分: 2798
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-08 20:15
文章分类

全部博文(264)

文章存档

2011年(42)

2010年(213)

2009年(4)

2008年(2)

2007年(3)

分类: C/C++

2011-05-27 10:20:33

写给刚入门的。


  1. #define MAX_MSG_LEN 64
  2. #define MAX_ARRY_SIZE 2

  3. //定义结构
  4. typedef struct ProtoMsg
  5. {
  6.     char msgData[MAX_MSG_LEN];
  7.     bool isOk;
  8.     char msgLen;
  9. }ProtoMsg_t; //你可以把ProtoMsg_t 看出数据类型用,像int,等数据类型。

  10. // ! 定义一个struct ProtoMsg的数组, 是不是和 int arry[MAX_ARRY_SIZE] 很像???
  11. // ! 所以,你完全可以把结构体当做数据类型使用。
  12. // ! 不带初始化的,相当于 int arry[2];
  13. ProtoMsg_t espMsgArry[MAX_ARRY_SIZE];

  14. // ! 另外一个版本,带初始化的。相当于 int arry[2] = { 1, 100 };
  15. ProtoMsg_t espMsgArry[MAX_ARRY_SIZE] =
  16. {
  17.     { "sdsd", 1, 2 },
  18.     { "sdsd", 1, 2 }
  19. };

如果结构体中不直接告诉数组大小用指针呢?

  1. typedef struct ProtoMsg
  2. {
  3.     char *pMsgData;
  4.     bool isOk;
  5.     char msgLen;
  6. }ProtoMsg_t;
该怎样初始化?

  1. ProtoMsg_t espMsgArry[MAX_ARRY_SIZE] =
  2. {
  3.     { "sdsd", 1, 2 },
  4.     { "sdsd", 1, 2 }
  5. };
初始化是完全一样。



阅读(5357) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~