INFO: C2106 Error Assigning a String Literal to a char Array (36870)
The information in this article applies to:
- Microsoft C for MS-DOS 6.0
- Microsoft C for MS-DOS 6.0a
- Microsoft C for MS-DOS 6.0ax
- Microsoft C/C++ for MS-DOS 7.0
- Microsoft Visual C++ 1.5
- Microsoft Visual C++ 1.0
- Microsoft Visual C++ 2.0
- Microsoft Visual C++ 2.1
- Microsoft Visual C++ 4.0
- Microsoft Visual C++, 32-bit Enterprise Edition 5.0
- Microsoft Visual C++, 32-bit Professional Edition 5.0
- Microsoft Visual C++, 32-bit Enterprise Edition 6.0
- Microsoft Visual C++, 32-bit Professional Edition 6.0
- Microsoft Visual C++, 32-bit Learning Edition 6.0
This article was previously published under Q36870 SUMMARY
A common programming error in C involves an attempt to fill a
character array, declared as "char arrayname[somelength]," with a
string constant by using the simple-assignment operator (the equal
sign, "="). This attempt fails and the compiler generates the
following message:
error C2106: '=' : left operand must be lvalue
Simple assignment works to fill non-auto (global) character arrays and
character pointers (in C version 5.0 and later) with static text. The
required declarations are as follows:
char string1[10] = "String1";
char *string2 = "String2";
The text below presents two code examples to demonstrate correct and
incorrect methods to initialize strings.
Modification Type: | Major | Last Reviewed: | 12/1/2003 |
---|
Keywords: | kberrmsg kbinfo kbLangC KB36870 |
---|
|