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.

MORE INFORMATION

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

Sample Code

/*
 * Compile options needed: None
 */ 

#include <string.h>
char string1[10];

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

Sample Code

/*
 * Compile options needed: None
 */ 

/* This example demonstrates some string usage principles. */ 
#include <stdio.h>
#include <string.h>
#include <malloc.h>

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 = "String1"; */ 
   strcpy(string1, "Contents of string1");
   printf("1:%s\n\n", string1);

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

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

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

   /* This shows how correctly to use a pointer   */ 
   /* to fill a char array with a string literal. */ 
   string2 = string1;
   strcpy(string2, "Contents of string2, and also string1");
   printf("6:%s\n", string1);
   printf("7:%s\n\n", 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
				

Modification Type:MajorLast Reviewed:12/1/2003
Keywords:kberrmsg kbinfo kbLangC KB36870