我是java开发人员,我想学习c++。现在我正在学习结构体。我看到了一个在互联网中使用结构体(https://www.tutorialspoint.com/cplusplus/cpp_data_structures.htm)的例子。
#include <iostream>
#include <cstring>
using namespace std;
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( ) {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
...
return 0;
}这段代码是有效的,但是我有一个问题:为什么编译器不允许使用行Book1.title = "Learn C++ Programming";,但是却允许使用行strcpy( Book1.title, "Learn C++ Programming");。为什么Book1.title = "Learn C++ Programming";和Book1.book_id = 6495407;不同(除了诅咒的类型)?
发布于 2017-07-19 02:50:27
我只会使用std::string而不是字符数组。除非有某些特定的原因需要使用数组,否则std::string类型的功能要多得多。
https://stackoverflow.com/questions/45173113
复制相似问题