INFO: C2106 Error Assigning a String Literal to a char ArrayLast reviewed: October 1, 1997Article ID: Q36870 |
The information in this article applies to:
SUMMARYA 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 lvalueSimple 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 INFORMATIONThe 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 Keywords : CLngIss kberrmsg kbfasttip Version : MS-DOS:6.0,6.00a,6.00ax,7.0; WINDOWS:1.0,1.5; WINDOWS NT:1.0,2.0,2.1,4.0,5.0 Platform : MS-DOS NT WINDOWS Issue type : kbinfo |
================================================================================
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |