pinery
驱动小牛
驱动小牛
  • 注册日期2005-04-14
  • 最后登录2013-04-07
  • 粉丝0
  • 关注0
  • 积分14分
  • 威望283点
  • 贡献值0点
  • 好评度77点
  • 原创分0分
  • 专家分0分
阅读:1504回复:1

麻烦帮看看函数调用中这两个数组什么地方出了问题?

楼主#
更多 发布于:2007-03-20 22:55
  麻烦帮看看函数调用中这两个数组什么地方出了问题?vc6.0下编译
我这个程序为什么aa数组和bb数组中的数据不一样,在调用sendstring()函数的时候输出bb数组的时候aa数组中的内容也跟在后面了? 谢谢....

#include <windows.h>
#include <iostream.h>
#include <stdio.h>
void strcopy(unsigned char *destr,unsigned char *soustr)
{
    int e = 0;
    for(e=0; soustr[e]!=0xfa&&soustr[e]!='\0'; e++)
        destr[e] = soustr[e];
    destr[e] = '\0';
}

void sendstring(unsigned char *str)
{
    unsigned char kd[20];
    for(int ecount=0; ecount<20; ecount++)
        kd[ecount] = '\0';
    //cout << kd << endl;

    strcopy(kd,str);
    for(unsigned int i=0; i<strlen((const char *)kd); i++)
        printf("%x ",kd);
    //cout << kd <<" " << endl;
}

void sendfirst(void)
{
    unsigned char aa[5] = {0xaa,0xe0,0x45,0x01,0x76};
    unsigned char bb[6] = {0xaa,0x10,0x45,0x02,0x7f,0x03};
    unsigned char e[20];
    int count = 0;

    sendstring(aa);
    for(count=0; count<20; count++)
        e[count] = '\0';
    sendstring(bb);

void main(void)
{
    sendfirst();
}
 程序运行的结果是:
  aa e0 45 1 76 cc cc cc 89 ff 12
  aa 10 45 2 7f 3  cc cc aa e0 45 1 76 cc cc cc 89 ff 12
wince_lover
驱动中牛
驱动中牛
  • 注册日期2007-01-10
  • 最后登录2010-12-20
  • 粉丝0
  • 关注0
  • 积分1103分
  • 威望735点
  • 贡献值2点
  • 好评度279点
  • 原创分1分
  • 专家分0分
沙发#
发布于:2007-03-21 09:32
因为AA BB两个数组没有以'\0'或者0xfa结尾,strcopy的时候数组越界了
游客

返回顶部