C Language Execution times error "The expression must be a modifiable lvalue and cannot be converted from" const char [3] "to" char [120] ", because a string cannot be directly assigned to a value

Source: Internet
Author: User

Workaround: Use the strcpy function for string copy prototype declaration: Char *strcpy (char* dest, const char *src); header file: #include <string.h> and #include < stdio.h> function: Copy a string starting from the SRC address and containing a null terminator to the address space beginning with dest: src and dest memory areas cannot overlap and dest must have enough space to accommodate the SRC string. Returns a pointer to the dest.
1 //testArray.cpp: Defines the entry point of the console application. 2 3#include"stdafx.h"4#include"string.h"5 6 #defineMax_age_size 1207 #defineMax_name_size 1008 9typedefenum{//enumeration value DefinitionsTenWalking =1, Onerunning =2, ASwimming =3, -Jumpping =4, -Sleeping =5 the }hobby; -  -typedefenum{ -Chinese =1, +中文版 =2, -Japanese =3 + }language; A  attypedefstructpeople{//definition of structural body -union{//Union definition, use method and struct similar -         struct{ -             CharAge[max_age_size]; -             CharName[max_name_size]; - }child; in Hobby Hobby; - Language Language; to }student;  + }people; -  the  * int_tmain (intARGC, _tchar*argv[]) $ { Panax Notoginseng     CharName1[max_name_size] = {"test1"}; -     CharName2[max_name_size] = {"test2"}; the  +People p[2]; A     //P[0]. Student.Child.age = "Ten";//Error: The expression must be a modifiable lvalue (reason: string cannot be assigned directly) thestrcpy (p[0]. Student.Child.age,"Ten");//using the strcpy function for string copying +strcpy (p[0]. STUDENT.CHILD.NAME,NAME1); -p[0]. Student.hobby =walking; $p[0]. Student.language =Chinese; $  -strcpy (p[1]. Student.Child.age," A"); -strcpy (p[1]. STUDENT.CHILD.NAME,NAME2); thep[1]. Student.hobby =running; -p[1]. Student.language =中文版;Wuyi  theprintf"Student1 ' s name:%s\n", p[0]. Student.Child.name); -     return 0; Wu}

C language Execution times error "The expression must be a modifiable lvalue and cannot be converted from" const char [3] "to" char [120] ", because the string cannot be directly assigned

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.