关于c_str()和char类型应用于函数参数的小问题!
需要调用的函数定义如下:
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;
小问题一个,请教高手!
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;
小问题一个,请教高手!
...全文
相关推荐
c++中c_str()的用法示例
//标准库的string类提供了三个成员函数来从一个string得到c类型的字符数组 //主要介绍c_str //c_str():生成一个const char*指针,指向以空字符终止的数组。 //这个数组应该是string类内部的数组 #include //需要包含cstring的字符串 #include using namespace std; int main() { //string-->char* //c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同 //这个数组的数据是临时的,当有一个改变这些数据的成员函数
c_str()的用法详细解析
c_str()就是把string类对象转换成和c兼容的char *类型。这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式
c++中string类成员函数c_str()的用法
c_str()函数返回一个指向正规c字符串的指针,内容和string类的本身对象是一样的,通过string类的c_str()函数能够把string对象转换成c中的字符串的样式
C语言中二维数组作为函数参数来传递的三种方法
主要给大家介绍了关于C语言中二维数组作为函数参数来传递的三种方法,文中通过示例代码介绍的非常详细,对大家学习或者使用C语言有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
详解C语言中的char数据类型及其与int类型的转换
主要介绍了详解C语言中的char数据类型及其与int类型的转换,是C语言入门学习中的基础知识,需要的朋友可以参考下


