C++之结构体

定义结构体

C++ 数组允许定义可存储相同类型数据项的变量,但是结构体是 C++ 中另一种用户自定义的可用的数据类型,它允许程序存储不同类型的数据项。

#include <iostream>
#include <cstdio>
#include <ctime>
#include <cstring>

using namespace std;

#Books结构体名字,根据Books可以定义结构体的变量book,book1,book2
struct Books {
	#注意定义时,要考虑数组的大小,不然目标字符串长度大于被复制的数组长度时,会造成缓冲溢出
    char title[50];
    char author[50];
    char subject[50];
    int book_id;
} book;
struct Books book1;
struct Books book2;

int main() {
    system("chcp 65001");
    strcpy(book1.title, "c++");
    strcpy(book1.author, "mr.ji");
    strcpy(book1.subject, "programmer language");

    book1.book_id = 1;

    strcpy(book2.title, "python");
    strcpy(book2.author, "mr.ji");
    strcpy(book2.subject, "programmer language");

    book2.book_id = 2;
    cout << "the title of the first book:" << book1.title << endl;
    cout << "the author of the first book:" << book1.author << endl;
    cout << "the subject of the first book:" << book1.subject << endl;
    cout << "the book_id of the first book:" << book1.book_id << endl;
    cout << "the title of the second book:" << book2.title << endl;
    cout << "the author of the second book:" << book2.author << endl;
    cout << "the subject of the second book:" << book2.subject << endl;
    cout << "the book_id of the second book:" << book2.book_id << endl;

    return 0;
}
输出:
Active code page: 65001
the title of the first book:c++
the author of the first book:mr.ji
the subject of the first book:programmer language
the book_id of the first book:1
the title of the second book:python
the author of the second book:mr.ji
the subject of the second book:programmer language
the book_id of the second book:2

传结构体参数

#include <iostream>
#include <cstdio>
#include <ctime>
#include <cstring>

using namespace std;

struct Books {
    char title[50];
    char author[50];
    char subject[50];
    int book_id;
} book;
struct Books book1;
struct Books book2;

void print_book(struct Books books);

int main() {
    system("chcp 65001");
    strcpy(book1.title, "c++");
    strcpy(book1.author, "mr.ji");
    strcpy(book1.subject, "programmer language");
    book1.book_id = 1;
    print_book(book1);

    strcpy(book2.title, "python");
    strcpy(book2.author, "mr.ji");
    strcpy(book2.subject, "programmer language");
    book2.book_id = 2;
    print_book(book2);
    return 0;
}

void print_book(struct Books books) {
    cout << "the title of the second book:" << books.title << endl;
    cout << "the author of the second book:" << books.author << endl;
    cout << "the subject of the second book:" << books.subject << endl;
    cout << "the book_id of the second book:" << books.book_id << endl;
}
输出:
Active code page: 65001
the title of the second book:c++
the author of the second book:mr.ji
the subject of the second book:programmer language
the book_id of the second book:1
the title of the second book:python
the author of the second book:mr.ji
the subject of the second book:programmer language
the book_id of the second book:2

传结构体参数给函数,和传普通变量没区别,传入的时候,声明好类型即可,比如此例中传的结构体类型是struct Books。

指向结构体的指针

#include <iostream>
#include <cstdio>
#include <ctime>
#include <cstring>

using namespace std;

struct Books {
	#注意定义时,要考虑数组的大小,不然目标字符串长度大于被复制的数组长度时,会造成缓冲溢出
    char title[50];
    char author[50];
    char subject[50];
    int book_id;
} book;
struct Books book1;
struct Books book2;
//若定义其他的结构体则指针pointer不能指向Books变量的地址,比如struct b *pointer;
struct Books *pointer;
struct Books *ptr;

void print_book(struct Books *books);

int main() {
    system("chcp 65001");
    pointer = &book1;
    ptr = &book2;
    strcpy(pointer->title, "C++");
    strcpy(pointer->author, "Mr.Ji");
    strcpy(pointer->subject, "programmer language");
    pointer->book_id = 1;
    print_book(pointer);
    cout << "----------------------------------------------------" << endl;
    strcpy(ptr->title, "Python");
    strcpy(ptr->author, "Mr.Ji");
    strcpy(ptr->subject, "programmer language");
    ptr->book_id = 2;
    print_book(ptr);
    return 0;
}

void print_book(struct Books *books) {
    cout << "the title of the second book:" << books->title << endl;
    cout << "the author of the second book:" << books->author << endl;
    cout << "the subject of the second book:" << books->subject << endl;
    cout << "the book_id of the second book:" << books->book_id << endl;
}
输出:
Active code page: 65001
the title of the second book:C++
the author of the second book:Mr.Ji
the subject of the second book:programmer language
the book_id of the second book:1
----------------------------------------------------
the title of the second book:Python
the author of the second book:Mr.Ji
the subject of the second book:programmer language
the book_id of the second book:2

缓冲区溢出是一种异常现象,当软件向缓冲区中写入数据使缓冲区容量溢出时,会导致相邻存储器位置被覆盖。换句话说,过量的信息被传递到没有足够空间的容器中,而这些信息最终会替换相邻容器中的数据。

若定义结构体的时候使用typedef关键字,则声明结构体变量的时候,可以省略struct关键字。这和之前我们提到的用typedef 作为其他数据结构类型是一样的,比如typedef int *ptr;ptr a;这里ptr表示的是指向int类型的指针类型(ptr代替了int *)。
C ++ 中的 typedef struct 是会对上面代码中最后的 book部分产生两种影响:
第一是不用 typedef:book 就相当于一个变量了,可以直接调用结构体中的内容
第二种是用 typedef:book 部分是个结构体类型,在调用结构体之前必须创建结构体的变量

#include <iostream>
#include <cstdio>
#include <ctime>
#include <cstring>

using namespace std;

typedef struct Books {
    char title[50];
    char author[50];
    char subject[50];
    int book_id;
} book;
book book1;
book book2;
//若定义其他的结构体则指针pointer不能指向Books变量的地址,比如struct b *pointer;
book *pointer;
book *ptr;

void print_book(struct Books *books);

int main() {
    system("chcp 65001");
    pointer = &book1;
    ptr = &book2;
    strcpy(pointer->title, "C++");
    strcpy(pointer->author, "Mr.Ji");
    strcpy(pointer->subject, "programmer language");
    pointer->book_id = 1;
    print_book(pointer);
    cout << "----------------------------------------------------" << endl;
    strcpy(ptr->title, "Python");
    strcpy(ptr->author, "Mr.Ji");
    strcpy(ptr->subject, "programmer language");
    ptr->book_id = 2;
    print_book(ptr);
    return 0;
}

void print_book(struct Books *books) {
    cout << "the title of the second book:" << books->title << endl;
    cout << "the author of the second book:" << books->author << endl;
    cout << "the subject of the second book:" << books->subject << endl;
    cout << "the book_id of the second book:" << books->book_id << endl;
}

代码与上述代码相似,只不过是定义结构体时,用到了typedef 关键字修饰,使得book为结构体类型,使用book创建变量。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值