字符序列序列错误

Uzu*_*Dev 2 c++ char data-structures

我无法将定义长度的char序列数组存储到struct的对象中.我可以在没有定义字符长度或只使用字符串的情况下使其工作,但它只是困扰我为什么会发生这种情况.

代码:

#include <iostream>
#include <string>
using namespace std;

struct highscore {
char name[50];
int score;
char date[10];
} hstable[9];

void printtable (highscore show_tab) {
cout << show_tab.name << show_tab.score << show_tab.date;
};

void main() {
hstable[0].name = "Kyle ";
hstable[0].score = 100;
hstable[0].date = " 01/03/88 \n";

printtable (hstable[0]);
system("pause");
 return;

};
Run Code Online (Sandbox Code Playgroud)

错误:

错误C2440:'=':无法从'const char [6]'转换为'char [50]'1>没有可以进行此转换的上下文

错误C2440:'=':无法从'const char [12]'转换为'char [10]'

Mat*_*Mat 5

如果要执行此操作,则应使用标题中的strcpy(或strncpy)函数<cstring>.

strcpy(hstable[0].name, "Kyle ");
Run Code Online (Sandbox Code Playgroud)

但请考虑使用std::string而不是普通的char数组.

注意:char[10]太小而不能存储" 01/03/88 \n"为C字符串,因此您已经陷入了C字符串提供的许多陷阱之一(在这种情况下缓冲区溢出).