[英]C++ Borland char * and strcpy
char *dum[32];
strcpy(&dum,InstList->Lines->Text.c_str());
InstList
is a TMemo
of C++ Builder InstList
是C ++ Builder的TMemo
Why am I getting this error? 为什么会出现此错误?
[C++ Error] emulator.cpp(59): E2034 Cannot convert 'char * *' to 'char *' Full parser context emulator.cpp(56): parsing: void _fastcall TMain::Button1Click(TObject *)
[C ++错误] emulator.cpp(59):E2034无法将'char * *'转换为'char *'完整解析器上下文emulator.cpp(56):解析:void _fastcall TMain :: Button1Click(TObject *)
char *dum[32];
is an array of length 32, each element being a char*
. 是长度为32的数组,每个元素都是
char*
。 I guess you meant to write 我想你打算写
char dum[32];
This is an array of 32 char and you can then write: 这是一个32个字符的数组,然后您可以编写:
strcpy(dum, InstList->Lines->Text.c_str());
Make sure, of course, InstList->Lines->Text
is not so big that it overflows your buffer. 当然,请确保
InstList->Lines->Text
不会太大,以至于缓冲区溢出。
Of course, I'm not sure why you would need to use C strings in a C++ program. 当然,我不确定为什么您需要在C ++程序中使用C字符串。
You either use (prone to serious security problem called buffer overflow ) 您可以使用(容易发生称为缓冲区溢出的严重安全性问题)
char dum[32];
strcpy(dum,InstList->Lines->Text.c_str());
OR (much better since it works with any length without being prone to a serious security problem called buffer overflow ) 或(更好,因为它可以任意长度工作,而不会出现称为缓冲区溢出的严重安全性问题)
// 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());
EDIT - as per comments: 编辑-根据评论:
I am assuming that you are using an older BCB version which still has AnsiString
- if this is on a newer version UnicodeString
then the code could lead to "strange results" since unicode string take up multiple bytes per character (depending on the encoding etc.). 我假设您使用的仍然是
AnsiString
BCB旧版本-如果在较新的UnicodeString
版本UnicodeString
则该代码可能会导致“奇怪的结果”,因为Unicode字符串每个字符占用多个字节(取决于编码等)。 )。
char dum[32]; strcpy(dum,InstList->Lines->Text.c_str());
Do not use char*
use String
or std::string
instead and if you need a pointer to your string for some reason just take this from your string object. 不要使用
char*
来代替String
或std::string
,如果出于某种原因需要指向字符串的指针,只需从字符串对象中获取即可。
String myString = InstList->Lines->Text;
myString.c_str();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.