0

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
4
  • You can't assign to an array, but you can copy to it. But if you're programming in C++, why don't you use std::string? Commented Jan 25, 2016 at 7:20
  • You can also do something like char * m_manuf = "abacus"; probably. Commented Jan 25, 2016 at 7:21
  • @CollinD It's generally a bad idea to have a non-const pointer to a string literal. Commented Jan 25, 2016 at 7:28
  • @JoachimPileborg Oh absolutely. Commented Jan 25, 2016 at 7:34

2 Answers 2

2

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,

Sign up to request clarification or add additional context in comments.

Comments

0

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];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.