美文网首页
C++学习笔记5----结构体

C++学习笔记5----结构体

作者: ChineseBoy | 来源:发表于2017-06-22 18:33 被阅读14次
    struct Person{
        char name[22];
        bool sex;
        int age;
        double score;
    };
    
    typedef struct
    {
        char  title[50];
        char  author[50];
        char  subject[100];
        int   book_id;
    }Books;
    
    void printPerson(struct Person person){
        cout << "name = " << person.name << endl;
        cout << "sex = " << person.sex << endl;
        cout << "age = " << person.age << endl;
        cout << "score = " << person.score << endl;
    }
    
    void printPerson2(struct Person *person){
        cout << "name = " << person->name << endl;
        cout << "sex = " << person->sex << endl;
        cout << "age = " << person->age << endl;
        cout << "score = " << person->score << endl;
    }
    
    int main() {
        count = 5;
        write_extern();
    
        Person p1;
        Person p2;
        Books books;
    
        strcpy(books.author,"jack");
    
        strcpy(p1.name,"tom");
        p1.sex = false;
        p1.age = 11;
        p1.score = 89.5;
    
        strcpy(p2.name,"tom2");
        p2.sex = true;
        p2.age = 22;
        p2.score = 77.5;
    
        printPerson(p1);
        printPerson2(&p2);
        return 0;
    }
    

    相关文章

      网友评论

          本文标题:C++学习笔记5----结构体

          本文链接:https://www.haomeiwen.com/subject/cfnfcxtx.html