Microsoft KB Archive/36870

= INFO: C2106 Error Assigning a String Literal to a char Array =

Article ID: 36870

Article Last Modified on 12/1/2003

-

APPLIES TO


 * Microsoft C Professional Development System 6.0
 * Microsoft C Professional Development System 6.0a
 * Microsoft C Professional Development System 6.0a
 * Microsoft C/C++ Professional Development System 7.0
 * Microsoft Visual C++ 1.5 Professional Edition
 * Microsoft Visual C++ 1.0 Professional Edition
 * Microsoft Visual C++ 2.0 Professional Edition
 * Microsoft Visual C++ 2.1
 * Microsoft Visual C++ 4.0 Standard Edition
 * Microsoft Visual C++ 5.0 Enterprise Edition
 * Microsoft Visual C++ 5.0 Professional Edition
 * Microsoft Visual C++ 6.0 Enterprise Edition
 * Microsoft Visual C++ 6.0 Professional Edition
 * Microsoft Visual C++ 6.0 Standard Edition

-



This article was previously published under Q36870





SUMMARY
A common programming error in C involves an attempt to fill a character array, declared as &quot;char arrayname[somelength],&quot; with a string constant by using the simple-assignment operator (the equal sign, &quot;=&quot;). 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] = &quot;String1&quot;; char *string2 = &quot;String2&quot;; The text below presents two code examples to demonstrate correct and incorrect methods to initialize strings.



MORE INFORMATION
The following code example does not compile correctly and produces the C2106 error.

Sample Code
/* * Compile options needed: None */

char string1[10];
 * 1) include 

void main(void); void main(void) {  string1 = &quot;String1&quot;; } The following example demonstrates various methods to copy strings.

Sample Code
/* * Compile options needed: None */

/* This example demonstrates some string usage principles. */
 * 1) include 
 * 2) include 
 * 3) include 

char string1[40];      /* string1 is an array of char  */ char *string2;         /* string2 is a pointer to char */ /* Important: Know when to malloc space for string2. */

void main(void); void main(void) {  /* This shows the correct way to achieve the   */ /* assignment intended by string1 = &quot;String1&quot;; */ strcpy(string1, &quot;Contents of string1&quot;); printf(&quot;1:%s\n\n&quot;, string1);

/* These two assignments show two ways to    */ /* use a char pointer with a string literal. */   string2 = &quot;Contents of string2&quot;; /* point to the literal */ printf(&quot;2:%s\n&quot;, string2);

/* allocate memory for char *string2 to point at */ string2 = (char *)malloc(sizeof(string1)); strcpy(string2, &quot;Contents of string2, again&quot;); printf(&quot;3:%s\n\n&quot;, string2); free(string2);

/* This shows a failed attempt to fill a char */ /* array by assignment through a char pointer. */   string2 = string1; string2 = &quot;Contents of string2, but not string1&quot;; printf(&quot;4:%s\n&quot;, string1); printf(&quot;5:%s\n\n&quot;, string2);

/* This shows how correctly to use a pointer  */ /* to fill a char array with a string literal. */   string2 = string1; strcpy(string2, &quot;Contents of string2, and also string1&quot;); printf(&quot;6:%s\n&quot;, string1); printf(&quot;7:%s\n\n&quot;, string2); }

This code generates the following output:   1:Contents of string1

2:Contents of string2

3:Contents of string2, again

4:Contents of string1 5:Contents of string2, but not string1

6:Contents of string2, and also string1 7:Contents of string2, and also string1

Keywords: kberrmsg kbinfo kblangc KB36870

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.