结构体用->出错

本文讨论了在C++编程中遇到的结构体指针使用错误,并提供了有效的解决方案,帮助开发者理解如何正确地操作结构体指针。

typedef struct DATA{
        char numeric[100];
  int  top;
 }Data;  
 Data data;
 data->top=-1;

编译出错:

Compiling...
crypt.cpp
E:\workspace\crypt\crypt.cpp(67) : error C2819: type 'calculate::DATA' does not have an overloaded member 'operator ->'
        E:\workspace\crypt\crypt.cpp(62) : see declaration of 'DATA'
E:\workspace\crypt\crypt.cpp(67) : error C2227: left of '->top' must point to class/struct/union
E:\workspace\crypt\crypt.cpp(74) : error C2819: type 'calculate::DATA' does not have an overloaded member 'operator ->'
        E:\workspace\crypt\crypt.cpp(62) : see declaration of 'DATA'
E:\workspace\crypt\crypt.cpp(74) : error C2227: left of '->top' must point to class/struct/union
执行 cl.exe 时出错.

crypt.exe - 1 error(s), 0 warning(s)

 

->只能用于只想结构体的指针变量,所以需定义:

 Data *data;

data=(Data*)malloc(sizeof(Data)); 

if((data->etherHistoryIndex == eth_abc->e_data.etherHistoryIndex) &&(data->etherHistorySampleIndex == eth_abc->e_data.etherHistorySampleIndex)) { /* copy information :*/ data->etherHistoryIndex = eth_abc->e_data.etherHistoryIndex; data->etherHistorySampleIndex = eth_abc->e_data.etherHistorySampleIndex; data->etherHistoryIntervalStart = eth_abc->e_data.etherHistoryIntervalStart; data->etherHistoryDropEvents[0] = eth_abc->e_data.etherHistoryDropEvents[0]; data->etherHistoryDropEvents[1] = eth_abc->e_data.etherHistoryDropEvents[1]; data->etherHistoryOctets[0] = eth_abc->e_data.etherHistoryOctets[0]; data->etherHistoryOctets[1] = eth_abc->e_data.etherHistoryOctets[1]; data->etherHistoryPkts[0] = eth_abc->e_data.etherHistoryPkts[0]; data->etherHistoryPkts[1] = eth_abc->e_data.etherHistoryPkts[1]; data->etherHistoryBroadcastPkts[0] = eth_abc->e_data.etherHistoryBroadcastPkts[0]; data->etherHistoryBroadcastPkts[1] = eth_abc->e_data.etherHistoryBroadcastPkts[1]; data->etherHistoryMulticastPkts[0] = eth_abc->e_data.etherHistoryMulticastPkts[0]; data->etherHistoryMulticastPkts[1] = eth_abc->e_data.etherHistoryMulticastPkts[1]; data->etherHistoryCRCAlignErrors[0] = eth_abc->e_data.etherHistoryCRCAlignErrors[0]; data->etherHistoryCRCAlignErrors[1] = eth_abc->e_data.etherHistoryCRCAlignErrors[1]; data->etherHistoryUndersizePkts[0] = eth_abc->e_data.etherHistoryUndersizePkts[0]; data->etherHistoryUndersizePkts[1] = eth_abc->e_data.etherHistoryUndersizePkts[1]; data->etherHistoryOversizePkts[0] = eth_abc->e_data.etherHistoryOversizePkts[0]; data->etherHistoryOversizePkts[1] = eth_abc->e_data.etherHistoryOversizePkts[1]; data->etherHistoryFragments[0] = eth_abc->e_data.etherHistoryFragments[0]; data->etherHistoryFragments[1] = eth_abc->e_data.etherHistoryFragments[1]; data->etherHistoryJabbers[0] = eth_abc->e_data.etherHistoryJabbers[0]; data->etherHistoryJabbers[1] = eth_abc->e_data.etherHistoryJabbers[1]; data->etherHistoryCollisions[0] = eth_abc->e_data.etherHistoryCollisions[0]; data->etherHistoryCollisions[1] = eth_abc->e_data.etherHistoryCollisions[1]; data->etherHistoryUtilization = eth_abc->e_data.etherHistoryUtilization; return TRUE; } 这段代码是否可以简化?
11-26
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值