标签:const printf htm english 枚举 头文件 控制台 cpp walk
1 // testArray.cpp : 定义控制台应用程序的入口点。 2 3 #include "stdafx.h" 4 #include "string.h" 5 6 #define MAX_AGE_SIZE 120 7 #define MAX_NAME_SIZE 100 8 9 typedef enum{//枚举值定义 10 walking = 1, 11 running = 2, 12 swimming = 3, 13 jumpping = 4, 14 sleeping = 5 15 }Hobby; 16 17 typedef enum{ 18 Chinese = 1, 19 English = 2, 20 Japanese = 3 21 }Language; 22 23 typedef struct People{//结构体定义 24 union{//联合体定义,使用方法和struct类似 25 struct{ 26 char age[MAX_AGE_SIZE]; 27 char name[MAX_NAME_SIZE]; 28 }Child; 29 Hobby hobby; 30 Language language; 31 }Student; 32 }People; 33 34 35 int _tmain(int argc, _TCHAR* argv[]) 36 { 37 char name1[MAX_NAME_SIZE] = {"test1"}; 38 char name2[MAX_NAME_SIZE] = {"test2"}; 39 40 People p[2]; 41 //p[0].Student.Child.age = "10";//报错:表达式必须是可修改的左值(原因:字符串不能直接赋值 ) 42 strcpy(p[0].Student.Child.age,"10");//使用strcpy函数实现字符串拷贝 43 strcpy(p[0].Student.Child.name,name1); 44 p[0].Student.hobby = walking; 45 p[0].Student.language = Chinese; 46 47 strcpy(p[1].Student.Child.age,"12"); 48 strcpy(p[1].Student.Child.name,name2); 49 p[1].Student.hobby = running; 50 p[1].Student.language = English; 51 52 printf("Student1‘s name:%s\n",p[0].Student.Child.name); 53 return 0; 54 }
C语言执行时报错“表达式必须是可修改的左值,无法从“const char [3]”转换为“char [120]” ”,原因:字符串不能直接赋值
标签:const printf htm english 枚举 头文件 控制台 cpp walk
原文地址:http://www.cnblogs.com/wanglin2016/p/6041071.html