清空结构体
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
最大成员空间决定。操作的是同一空间。(类似指针))
每个成员可操作空间是由本身决定的。
引用
内联函数
- 减少函数调用的开销。 编译阶段替换