打印

C编程的时候的问题

[复制链接]
1609|4
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
ttaid|  楼主 | 2011-1-24 12:27 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
typedef struct
{
  unsigned short groupId;
  unsigned short clusterId;
} afIncomingMSGPacket_t;
afIncomingMSGPacket_t *MSGpkt=0x20;//表示MSGpkt指针式指向0x20这个地址
   MSGpkt->groupId=0x0304;
   MSGpkt->clusterId=0x0102;
给相对的地址中的变量赋值,但是如果不指定地址,即MSGpkt=0x20,
   MSGpkt->groupId=0x0304;
   MSGpkt->clusterId=0x0102;
会出错
如下
typedef struct
{
  unsigned short groupId;
  unsigned short clusterId;
} afIncomingMSGPacket_t;
afIncomingMSGPacket_t *MSGpkt;//不给MSGpkt赋值
  MSGpkt->groupId=0x0304;
   MSGpkt->clusterId=0x0102;
最后 groupId=0x0300
dusterId=0x0102

不知什么原因,求解~~

相关帖子

沙发
mmax| | 2011-1-24 12:36 | 只看该作者
晕,不赋值的指针是不能用的。

使用特权

评论回复
板凳
ttaid|  楼主 | 2011-1-24 12:52 | 只看该作者
这样啊,那一般对这种结构赋值采用什么形式和过程呢,不可能是直接自己写个指针这样子吧,请教下啊,谢谢咯 2# mmax

使用特权

评论回复
地板
mmax| | 2011-1-24 12:58 | 只看该作者
先定义个变量,再定义一个指针。
afIncomingMSGPacket_t  structTemp;
afIncomingMSGPacket_t  *MSGpkt;
MSGpkt= &structTemp;
这样,这个指针才会有实体内容。
后续的程序要调用,就直接
  MSGpkt->groupId=0x0304;
   MSGpkt->clusterId=0x0102;

指针不推荐直接赋地址值。程序简单的时候,你多个变量还能搞清楚。程序大了后,还是让编译器来分配变量地址吧。

使用特权

评论回复
5
ttaid|  楼主 | 2011-1-24 13:01 | 只看该作者
恩~~懂了,谢谢啦~~ 4# mmax

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

38

主题

112

帖子

1

粉丝