清空结构体
memset(&lucky, 0, sizeof(lucky))
结构体赋值问题
#pragma execution_character_set("utf-8")
#include <QtCore/QCoreApplication>
#include <QtCore/QCommandLineParser>
#include <QtCore/QCommandLineOption>
#include "echoclient.h"
#include <iostream>
#include <stdio.h>
using namespace std;
extern void log(string str);
struct Book {
int id;
char name[20];
int time;
};
void addBook(Book *book){
log(book -> name);
}
void takeInfo(Book *book) {
// int n = sizeof(*book)/sizeof((*book)[0]);
cout << (book + 1)->id;
for (int i = 0; i < 3; i ++) {
cout << "input the" << i << "book" << endl;
cin >> (book +1) -> id >> (book +i)->name >> (book + i)-> time;
cout << "end input the" << i << "book"<< endl;
}
}
void sortInfo(Book *book) {
for (int i = 0; i < 2; i ++) {
if((book + i) ->time > (book + i + 1) -> time) {
Book temp = *(book+i);
*(book + i) = *(book +i +1);
*(book+i+1) = temp;
}
}
}
void showInfo(Book *book) {
for (int i = 0; i < 3; i ++) {
cout << (book +i)->id << " " << (book +i)->name << " " << (book +i)->time << endl;
}
}
int main(int argc, char *argv[])
{
Book book[3] = {{123, "yolov3", 3}, {234, "yolov5", 2023}, {333, "car", 555}};
// takeInfo(book);
sortInfo(book);
showInfo(book);
}
共用体
union
最大成员空间决定。操作的是同一空间。(类似指针))
每个成员可操作空间是由本身决定的。

引用


内联函数
- 减少函数调用的开销。 编译阶段替换

该博客介绍了C++中结构体的使用,包括如何清空结构体,以及结构体数组的赋值、排序和显示。通过`memset`函数初始化结构体,`addBook`、`takeInfo`、`sortInfo`和`showInfo`函数展示了结构体成员的操作。此外,还提及了共用体的概念和特点。
1766

被折叠的 条评论
为什么被折叠?



