关于c_str()和char类型应用于函数参数的小问题!
jukyy 2003-04-15 01:43:49 需要调用的函数定义如下:
BOOL __fastcall TCommunication::WriteCommBlock(LPSTR lpBlock,DWORD nMaxLength)
在如下调用时不会出错(其中sendBuffer是AnsiString类型)
bool bCode=COMX->WriteCommBlock(sendBuffer.SubString(sendBuffer.Length()-sendCount+1,1).c_str(),1);
if(bCode) sendCount = sendCount-1;
而我想这样调用该函数:
char cc;
strtemp = sendBuffer.SubString(sendBuffer.Length() - sendCount + 1,2);
intlen = sendBuffer.Length();
cc = char(StrToInt("0x" + strtemp));
bCode=COMX->WriteCommBlock(cc,2); //编译出错,不能int转换成char
if(bCode) sendCount = sendCount-2;
当我按照下面这样定义调用时编译可以通过,但是我不知道结果是否对了?
char *cc;
strtemp = sendBuffer.SubString(sendBuffer.Length() - sendCount + 1,2);
intlen = sendBuffer.Length();
*cc = char(StrToInt("0x" + strtemp));
bCode=COMX->WriteCommBlock(cc,2); //编译出错,不能int转换成char
if(bCode) sendCount = sendCount-2;
小问题一个,请教高手!