user3514797
user3514797

Reputation: 47

C++ char array: error while copying data

I have an array:

CHAR m_manuf[256];

I am trying to copy a value to this array as:

m_manuf = "abacus";  //This shows error 

I also tried this variation:

char abc[256] ="abacus";
m_manuf = abc; //Shows error as left value must be l-value

Upvotes: 0

Views: 147

Answers (2)

Roger
Roger

Reputation: 1225

A constant char array is good enough for you so you go with,

string tmp = "abacus";
char *new = tmp.c_str();

Or you need to modify new char array and constant is not ok, then just go with this

char *new = &tmp[0];

Upvotes: 0

Akhil V Suku
Akhil V Suku

Reputation: 912

You cant' copy an array like that, instead of you can do,

CHAR    m_manuf[256];
strcpy(m_manuf,"abacus" );

Or

char * m_manuf = "abacus";

Or

char abc[256] ="abacus";
strcpy(m_manuf,abc );

Note : The better way to handle char arrays are using std::string,

Upvotes: 2

Related Questions