Simple string assignment problem

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Simple string assignment problem

985 次查看
protoease
Contributor II

Banging my head against the wall on the following simple character string assignment problem:

 

char user_text[6];

 

// somewhere else in code:

user_text[] = {"text"};

 

Compiler throws up alarm on C1830: Modifiable lvalue expected.

 

What am I doing wrong?

 

I don't want to include string support as I can't support the burden size-wise.

 

Appreciate any help, I'm sure it is something silly and simple.

 

Thanks,

Aaron

标签 (1)
0 项奖励
回复
1 回复

696 次查看
bigmac
Specialist III

Hello Aaron,

 

Your string assignment is not allowable in C.  With this format, a constant or variable may only be initialised, i.e. at the same time the constant or variable is defined.

 

To assign data to an array variable within a function, each element of the array woulld need to be individually written.

user_text[0] = 't';

user_text[1] = 'e';

user_text[2] = 'x';

user_text[3] = 't';

user_text[4] = 0;

 

To copy constant string data into the array variable, you will need to write your own function if you do not wish to include an existing library within your project.  Keep in mind that only the code size associated with the particular library functions that you use, will be added to your project.

 

Regards,

Mac

0 项奖励
回复