本文介绍了错误:calcChksum重新编译时类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include<stdio.h>
#include<conio.h>

int main(short argc, char** argv)
{

   unsigned char serNum [] = "DS0008D-0111A";
   unsigned char *ser = serNum;
   unsigned short checksum;
checksum = CalcChksum(*ser,13);
printf("the checksum is %x",checksum)  ;
return 0;
}


unsigned short CalcChksum(unsigned char *ptr,int bytecnt)
{
   int i;
   unsigned short chksum;

   chksum = 0;
   // Calculate 1's complement chksum
   for(i=0; i<bytecnt; i++)
   {
   chksum += (unsigned short)*ptr;
   chksum = (char)~chksum;
   }
   return(chksum);

}

推荐答案

unsigned char *ser = serNum;
checksum = CalcChksum(*ser,13);



不正确, CalcChksum 声明为;


Is incorrect, CalcChksum is declared like;

unsigned short CalcChksum(unsigned char *ptr, int bytecnt)



所以第一个参数应该是 char * ,通过在 ser 前面加上星号,你可以从 unsigned char * 中取消引用它到 unsigned char

将呼叫更改为


So the first parameter should be a char*, by putting as asterisk in front of ser you're de-referencing that from an unsigned char * to a unsigned char.
Change the call to

checksum = CalcChksum(ser,13);



希望这会有所帮助,

Fredrik


Hope this helps,
Fredrik


这篇关于错误:calcChksum重新编译时类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 09:29