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

InstList is a TMemo of C++ Builder

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 *)

BeNdErR
  • 17,471
  • 21
  • 72
  • 103
Hakon89
  • 1,011
  • 2
  • 9
  • 17

4 Answers4

2
char *dum[32];

is an array of length 32, each element being a char*. I guess you meant to write

char dum[32];

This is an array of 32 char and you can then write:

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

Make sure, of course, InstList->Lines->Text is not so big that it overflows your buffer.

Of course, I'm not sure why you would need to use C strings in a C++ program.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

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.).

Yahia
  • 69,653
  • 9
  • 115
  • 144
  • Good points, but no need even to use `strlen` since `InstList->Lines->Text` comes with a length property for sure. Not sure on the C++ Builder syntax mind you. – David Heffernan Oct 29 '11 at 11:52
  • @DavidHeffernan Last time I used C++ Builder is several years ago... looked it up... – Yahia Oct 29 '11 at 11:55
  • According to the docs, `Text` is `System::UnicodeString` and you would write `Length(System::UnicodeString)` from Delphi, I assume that C++ Builder is the same. `Text` is dynamically generated from the contents of the Windows EDIT control that is behind the TMemo. I'd probably read it out into a `System::UnicodeString` variable first. One can only wonder where C strings come into the equation. – David Heffernan Oct 29 '11 at 11:57
  • @DavidHeffernan you are right for the current versions... but for example C++ Builder 6(!) has this as `AnsiString` defined... unbelievable but V 6 is still in use... when I saw the C string handling I thought of the old versions, otherwise this code would do no good... – Yahia Oct 29 '11 at 12:00
  • @DavidHeffernan checked the docs... `System::UnicodeString` would return `wchar_t *` as a result of `c_str()`... given the error message the OP posted this seems to be an older version with `AnsiString`... – Yahia Oct 29 '11 at 12:04
  • Yeah, you're right. I didn't know you could do `::Length()` and `::c_str()` on a C++Builder `AnsiString` or indeed `UnicodeString`. None of that available for us Delphi users! There were would write `Length(str)` and `PChar(str)`. Anyway, I think we've got this Q covered. – David Heffernan Oct 29 '11 at 12:08
1
char dum[32];   
strcpy(dum,InstList->Lines->Text.c_str()); 
Alex F
  • 42,307
  • 41
  • 144
  • 212
1

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.

String myString = InstList->Lines->Text;
myString.c_str();
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
AlexTheo
  • 4,004
  • 1
  • 21
  • 35