C++基础(十三)C++的结构体指针中出错:表达式必须是可修改的左值

一、问题

https://zhidao.baidu.com/question/1605252159728939107.html

https://blog.csdn.net/Scurry_lz/article/details/80875647

出错原因:

字符数组不能用“=”赋值给另一数组,即name=str是不行的。

二、字符数组的赋值

只能用strcpy进行赋值

附上完整代码:

#include "stdafx.h"
#include<stdio.h>
#include "string"
#include "iostream"
struct stu   // 定义一个结构体
{
	char name[10];  // 姓名
	int num;  // 学号
	int age;  // 年龄
};
int main()
{
	struct stu *s;   // 定义一个结构体指针
	char str[] = "ZhangLi";
	//s->name = str;     // 对结构体中的成员变量name进行赋值
	strcpy(s->name, str);
	s->num = 2015120;  // 对结构体中的成员变量num进行赋值
	s->age = 18;       // 对结构体中的成员变量age进行赋值
	return 0;
}

猜你喜欢

转载自blog.csdn.net/xpj8888/article/details/85837148