zoukankan      html  css  js  c++  java
  • 结构体 typedef关键字

    1 结构体
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    void printBook( struct Book book );
    
    
    struct Book
    {
        char title[50];
        char author[50];
        char subject[100];
        int book_id;
    };
    
    
    int main()
    {
        struct Book book1;
        struct Book book2;
    
        strcpy( book1.title, "Learn c++ Programming");
        strcpy( book1.author, "Chand Miyan");
        strcpy( book1.subject, "c++ Programming");
        book1.book_id = 6495407;
    
        strcpy( book2.title, "Telecom Billing");
        strcpy( book2.author, "Yakit Singha");
        strcpy( book2.subject, "Telecom");
        book2.book_id = 6495700;
    
    
        cout << book1.title << " " << book1.author << " " << book1.subject << " " << book1.book_id << endl;
        cout << book2.title << " " << book2.author << " " << book2.subject << " " << book2.book_id << endl;
    
        cout << "传入函数打印" << endl;
    
    
        printBook(book1);
        printBook(book2);
    
    }
    
    void printBook( struct Book book )
    {
        cout << book.title << " " << book.author << " " << book.subject << " " << book.book_id << endl;
    }
    
    
    /* vim: set ts=4 sw=4 sts=4 tw=100 */
    2 指向结构的指针
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    
    typedef struct
    {
        char title[50];
        char author[50];
        char subject[100];
        int book_id;
    }Book;
    
    void printBook(Book *book);
    
    int main()
    {
        Book book1, book2;
    
        strcpy( book1.title, "Learn c++ Programming");
        strcpy( book1.author, "Chand Miyan");
        strcpy( book1.subject, "c++ Programming");
        book1.book_id = 6495407;
    
        strcpy( book2.title, "Telecom Billing");
        strcpy( book2.author, "Yakit Singha");
        strcpy( book2.subject, "Telecom");
        book2.book_id = 6495700;
    
        printBook(&book1);
        printBook(&book2);
    
    
        long int a = 100;
        typedef long int *ptrLInt;
        
        ptrLInt x;
        x = &a;
    
        cout << *x;
    
    }
    
    
    void printBook(Book *book)
    {
        //指向该结构的指针访问结构的成员,您必须使用 -> 运算符
        cout << book->title << " " << book->author << " " << book->subject << " " << book->book_id << endl;
    }
    
    /* vim: set ts=4 sw=4 sts=4 tw=100 */
  • 相关阅读:
    Unity3d-反编译C#和提取资源
    让年轻程序员少走弯路的14个忠告
    Objective-C的陷阱与缺陷
    Android中处理Touch Icon的方案
    常用的Java代码汇总
    cocos2dx游戏资源加密之XXTEA
    9种常见的Android开发错误及解决方案
    Linux 系统常用命令汇总(三) 用户和用户组管理
    Linux 系统常用命令汇总(四) 程序和资源管理
    Linux 系统常用命令汇总(二) vi 文本编辑
  • 原文地址:https://www.cnblogs.com/i80386/p/4356337.html
Copyright © 2011-2022 走看看