繁体   English   中英

C ++ Borland char *和strcpy

[英]C++ Borland char * and strcpy

char *dum[32];
strcpy(&dum,InstList->Lines->Text.c_str());

InstList是C ++ Builder的TMemo

为什么会出现此错误?

[C ++错误] emulator.cpp(59):E2034无法将'char * *'转换为'char *'完整解析器上下文emulator.cpp(56):解析:void _fastcall TMain :: Button1Click(TObject *)

char *dum[32];

是长度为32的数组,每个元素都是char* 我想你打算写

char dum[32];

这是一个32个字符的数组,然后您可以编写:

strcpy(dum, InstList->Lines->Text.c_str());

当然,请确保InstList->Lines->Text不会太大,以至于缓冲区溢出。

当然,我不确定为什么您需要在C ++程序中使用C字符串。

您可以使用(容易发生称为缓冲区溢出的严重安全性问题)

char dum[32];
strcpy(dum,InstList->Lines->Text.c_str());

或(更好,因为它可以任意长度工作,而不会出现称为缓冲区溢出的严重安全性问题)

// C style
// char *dum = malloc(strlen(InstList->Lines->Text.c_str())+1); 

// BCB style...
char *dum = malloc(InstList->Lines->Text.Length()+1);  

// BEWARE: AFTER any malloc you should check the pointer returned for being NULL

strcpy(dum,InstList->Lines->Text.c_str());

编辑-根据评论:

我假设您使用的仍然是AnsiString BCB旧版本-如果在较新的UnicodeString版本UnicodeString则该代码可能会导致“奇怪的结果”,因为Unicode字符串每个字符占用多个字节(取决于编码等)。 )。

char dum[32];   
strcpy(dum,InstList->Lines->Text.c_str());

不要使用char*来代替Stringstd::string ,如果出于某种原因需要指向字符串的指针,只需从字符串对象中获取即可。

String myString = InstList->Lines->Text;
myString.c_str();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM