1.排序算法
void selectSort(void* ptr, int ele_size, int ele_num, int(*compare)(void*, void*)) {
char* temp = malloc(ele_size);
for (int i = 0; i < ele_num; ++i) {
int minOrmax = i;
for (int j = i + 1; j < ele_num; ++j) {
//拿到比较元素的首地址
char* pJ = (char*)ptr + j * ele_size;
char* pMinOrMax = (char*)ptr + minOrmax * ele_size;
if (compare(pJ, pMinOrMax)) {
minOrmax = j;
}
}
if (minOrmax != i) {
char* pMinOrMax = (char*)ptr + minOrmax * ele_size;
char* pI = (char*)ptr + i * ele_size;
//对下标为j和minOrmax的两个元素交换
memcpy(temp, pI, ele_size);
memcpy(pI, pMinOrMax, ele_size);
memcpy(pMinOrMax, temp, ele_size);
}
}
if (temp != NULL) {
free(temp);
temp = NULL;
}
}
int compareInt(void* d1, void* d2) {
int* p1 = (int*)d1;
int* p2 = (int*)d2;
return *p1 < *p2;
}
void test(){
int arr[] = { 7,4,9,2,1 };
selectSort(arr, sizeof(int), sizeof(arr) / sizeof(int), compareInt);
for (int i = 0; i < 5; ++i) {
printf("%d ", arr[i]);
}
printf("\n");
}
2.预处理指令
2.1 文本包含指令(#include)
2.2 宏定义
无参数的宏定义(宏常量)
带参数的宏定义(宏函数)
2.3 条件编译
2.4 一些特殊的宏
void doLogic(int* p) {
if (NULL == p) {
printf("%s 的 %d行出错!\n", __FILE__, __LINE__);
return;
}
}
3.库的基本概念
4.动态库和静态库流程
4.1 静态库的创建
//mylib.h
#pragma once
#ifdef __cplusplus
extern "C"{
#endif
int myAdd(int a, int b);
#ifdef __cplusplus
}
#endif
4.2 静态库的使用
4.3 静态库优缺点
4.4 动态库的创建
4.5 动态库的使用
隐式调用
显式调用
5.递归函数
void reverse(char* p) {
//确认递归退出条件
if (*p == '\0')
return;
reverse(p + 1);
printf("%c", *p);
}
void test(){
char* s = "abcdefg";
reverse(s);
}
void recurse(int val) {
//顺序打印8793的每个数字
if (val == 0)
return;
int ret = val / 10;
recurse(ret);
printf("%d ", val % 10);
}
void test(){
recurse(8793);
}
#include"linkList.h"
void reversePrintList(struct linkNode* pCur) {
if (NULL == pCur)
return;
reversePrintList(pCur->next);
printf("%d ", pCur->data);
}
void test(){
//初始化链表
struct linkNode* header = init_linkList();
//链表逆序打印
reversePrintList(header->next);
//销毁链表
destroy(header);
}
6.面向接口编程
//CSckImp.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//开发接口
//初始化
void init_CSckImp(void** handle);
//发送接口
void send_CSckImp(void* handle, unsigned char* sendData, int sendLen);
//接受接口
void recv_CSckImp(void* handle, unsigned char* recvData, int* recvLen);
//关闭接口
void close_CSckImp(void* handle);
//CSckImp.c
#include "CSckImp.h"
struct Info {
char data[1024];
int len;
};
//初始化
void init_CSckImp(void** handle) {
if (NULL == handle)
return;
struct Info* info = malloc(sizeof(struct Info));
memset(info, 0, sizeof(struct Info));
*handle = info;
}
//发送接口
void send_CSckImp(void* handle, unsigned char* sendData, int sendLen) {
if (NULL == handle)
return;
if (NULL == sendData)
return;
struct Info* info = (struct Info*)handle;
strncpy(info->data, sendData, sendLen);
info->len = sendLen;
}
//接受接口
void recv_CSckImp(void* handle, unsigned char* recvData, int* recvLen) {
if (NULL == handle)
return;
if (NULL == recvData)
return;
if (NULL == recvLen)
return;
struct Info* info = (struct Info*)handle;
strncpy(recvData, info->data, info->len);
*recvLen = info->len;
}
//关闭接口
void close_CSckImp(void* handle) {
if (NULL == handle)
return;
free(handle);
handle = NULL;
}
//main.c
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>//可计算偏移量
#include "CSckImp.h"
//定义接口
//初始化
typedef void(*init_CSocketProtocol)(void** handle);
//发送接口
typedef void(*send_CSocketProtocol)(void *handle, unsigned char *sendData, int sendLen);
//接受接口
typedef void(*recv_CSocketProtocol)(void* handle, unsigned char* recvData, int* recvLen);
//关闭接口
typedef void(*close_CSocketProtocol)(void* handle);
//业务代码
void frameWork(init_CSocketProtocol init,
send_CSocketProtocol send,
recv_CSocketProtocol recv,
close_CSocketProtocol close)
{
//初始化链接
void* handle = NULL;
init(&handle);
//发送数据
char buf[1024] = "好好学习";
int len = strlen(buf);
send(handle, buf, len);
//接受数据
char recvBuf[1024] = { 0 };
int recvLen = 0;
recv(handle, recvBuf, &recvLen);
printf("接收到的数据:%s\n", recvBuf);
printf("接收到的数据长度:%d\n", recvLen);
//关闭链接
close(handle);
handle = NULL;
}
void test(){
frameWork(init_CSckImp, send_CSckImp, recv_CSckImp, close_CSckImp);
}
int main(){
test();
return 0;
}