Microsoft KB Archive/36870

From BetaArchive Wiki
< Microsoft KB Archive
Revision as of 09:19, 21 July 2020 by X010 (talk | contribs) (Text replacement - ">" to ">")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Knowledge Base


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 "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
                

Keywords: kberrmsg kbinfo kblangc KB36870