C语言
C基础以及延申
扳手的海角
这个作者很懒,什么都没留下…
展开
-
204_C\C++_高级语言的反射机制,简单来说就是可以通过字符串型获取对应的类或者函数。下面用C来简单模拟反射
#include <stdio.h>#include <string.h>typedef void (*callback)(void);//给函数指针起别名typedef struct { const char *name; callback fn;}callback_t;void f0();void f1();callback_t callbacks[] = { {"cmd0", f0}, {"cmd1", f1},};v原创 2021-12-21 14:27:44 · 657 阅读 · 0 评论 -
203_C\C++_简单模拟委托,通过用指针函数作为地址接收函数地址,以达到委托其他函数实现某方法的目的
//C语言简单模拟委托//需要用的指针函数。通过用指针函数作为地址接收函数地址,以达到委托其他函数实现某方法的目的。#include <stdio.h>typedef void(* fun)(); //typedef 把void(*)()类型重命名为funvoid func(fun); // 被调函数void func_1(); // 回调函数1void func_2(); // 回调函数2int main() // 主函数用做主调函数{ func(func_1原创 2021-12-21 14:23:30 · 430 阅读 · 0 评论 -
162_附加145路由器项目中所用到的单链表操作_尾插、头插、遍历打印、查找ip、删除节点数据、释放全部、保存终端输入IP到文件、文件中的过滤IP插入到链表
动图:过滤IP的链表#include "ip_file.h"#define ip_config_name "ip_config"//与main.c共用一个结构体指针变量,保存过滤IP链表头节点MY_ROU * roulink_head = NULL;//--------------------操作文件中的过滤IP----------------------//void init_ip_link(){ FILE *ip_config = NULL; ip_config =原创 2021-10-24 16:54:19 · 131 阅读 · 0 评论 -
35_队列_先进后出,删除的话只能从头部向下删【压栈、出栈——仅能一个个删除】
main.c#include<stdio.h>#include<malloc.h>#include<stdlib.h>#include<stdbool.h>typedef struct Node{ int data; struct Node *pNext;}NODE,*PNODE;typedef struct Stack{ PNODE pTop; PNODE pBottom;}STACK,*PSTACK;//PSTACK等价于s原创 2021-08-10 21:30:53 · 84 阅读 · 0 评论 -
32_简单的宏制作(制作C语言下的宏,在C++下编译)
myadd.c无任何头文件存在!!int myadd(int a, int b){ return a+b;}main.cpp#include<iostream>using namespace std;//**这里以及 可以作为myadd()函数的头文件来使用了!!**//**无需再创建myadd.h文件**extern "C"{ int myadd(int a, int b);}int main(){ cout<< myad原创 2021-08-09 17:44:32 · 210 阅读 · 0 评论 -
31_C语言实现.lrc歌词滚动播放_2_【采用双链表,if 判断开头、接近开头、接近尾部、尾部、中间】--其余文件copy文章-28号
图示printf_link.c#include"file_link.h"void printf_link(LRC*phead, char **buf);void Free_link(LRC**phead);void xs(LRC *phead,int time_1);int main(){ FILE*fp; fp = fopen("/mnt/hgfs/ios2021/01_file/test.lrc","r+"); LRC*phead = NULL;原创 2021-08-07 10:00:37 · 490 阅读 · 0 评论 -
30_单链表的【增】【删】【改】【查】【Makefile】-详解版
main.c–其中的函数都在专门的.c.h文件中声明定义#include"HeadInsertSTUNode.h"#include"Title.h"#include"printf_link.h"#include"Free_link.h"#include"struct.h"#include"SearchNumNodeLink.h"#include"EndInsertSTUNode.h"int main(){ STD *phead = NULL;//这个其实就是一个空链表,头指针原创 2021-08-06 15:07:15 · 110 阅读 · 0 评论 -
29_双向链表の有序插入(加见解)
图示main.c#include<malloc.h>#include<stdio.h>#include<string.h>#include"link.h"int main(){ STD*phead = NULL; int num = 0; char name[218] = {0}; float sorce = 0; int userscmd= -1; while(1) { scan原创 2021-08-05 20:38:54 · 231 阅读 · 0 评论 -
28_C语言实现【双向链表】Linux下滚动播放.lrc歌词文件_1_,每次标红中间一行歌词,界面每次只显示5行【if中嵌套for循环的方式,每次循环5轮】
main.c#include"file_link.h"void printf_link(LRC*phead);void Free_link(LRC**phead);void xs(LRC *phead,int time_1);int main(){ FILE*fp; fp = fopen("/mnt/hgfs/ios2021/01_file/test.lrc","r+"); LRC*phead = NULL; char song[128] = {0};原创 2021-08-05 17:10:08 · 761 阅读 · 0 评论 -
27_c语言【有序单链表】读取.lrc歌词文件,显示出来时间以及对应歌词
main.c#include"file_link.h"void printf_link(LRC*phead);void Free_link(LRC*phead);int main(){ FILE*fp; fp = fopen("/mnt/hgfs/ios2021/01_file/test.lrc","r+"); LRC*phead = NULL; char song[128] = {0}; int time = 0; int len = 0;原创 2021-08-04 16:20:13 · 606 阅读 · 0 评论 -
26_对short类型的指针进行 [指针、值] の前++、后++(注意:步长!!!)
*p++:先对指针操作,因为++在后,所以先对p1取值得2,再第二步在对指针+1(p = p +1)*(p++) :因为++在后,所以先对p2取值,此时指针指向p2,因为short的步长和int不同,所以,对p2取值得0,再第三步在对指针+1(p = p +1)(p)++:先对指针进行取值,此时指向p3的位置,取值得3,再对p++(*p = *p +1)++(p):对第三步的p(4)取值的4,因为++在前,所以得5*++p:对指针操作,因为++在前,所以此时指向p4得位置,*p得0*(++p):对原创 2021-07-31 17:35:30 · 411 阅读 · 0 评论 -
25_手动输入随机数,与rand产生的随机数进行比对,错误的用其他字符替换,并且计算用时
#include<stdio.h>#include <stdlib.h> //srand(), rand()#include <time.h> //time()#include <conio.h>#include<string.h>void read_rand(char *a, char *buf_1);void compare(char * buf_1);int main(){ int i = 0; char a[27] =原创 2021-07-31 16:54:33 · 93 阅读 · 0 评论 -
24_把某个字符全部移动到字符串的尾部(采用了第三个中间变量指针)
#include<stdio.h>#include<string.h>void dissort(char *str, int n);void swap(char *start, char *next);int main(){ char str[128] = "\0na\0hello\0world\0\0\0b\0hei\0ha\0"; int count_num = sizeof(str); printf("%d\n", count_num); dissort(原创 2021-07-31 16:53:35 · 230 阅读 · 0 评论 -
23_结构体中包含另外一个结构体指针,为其进行二级malloc以及三级malloc(图解!!!)
#include<stdio.h>#include<string.h>#include<stdlib.h>#define _CRT_SECURE_NO_WARNINGStypedef struct student{ int id; char *name;}STU;typedef struct tercher{ STU *s; char *name;}TER;void test01(){ TER *p = (TER *)malloc(.原创 2021-07-29 16:21:18 · 1271 阅读 · 1 评论 -
22_结构体中创建一级malloc、二级malloc和三级malloc
注意:sizeof(p->student_name) 、/sizeof(p->student_name[0]这两个参数的都是指针类型,大小都是4!!#include<stdio.h>#include<string.h>#include<stdlib.h>#define _CRT_SECURE_NO_WARNINGStypedef struct _tercher{ int id; char *t_name; char **student_na原创 2021-07-29 15:43:49 · 90 阅读 · 0 评论 -
21_malloc分配一个大的数组,里面有3个小空间,小空间同样也malloc堆区空间
VS2017下–malloc分配内存#include<stdio.h>#include<string.h>void get_men(char ***p){ int i; char **temp ; //①定义一个中间变量temp temp = (char**)malloc(sizeof(char*) * 3);//②这一步为指向指针数组首行地址的地址分配3个大小的空间 //③其中的三个空间的地址是(*temp+i),都是原创 2021-07-29 11:38:10 · 134 阅读 · 0 评论 -
20_函数指针数组的拆解步骤***
函数(指针)指针数组:int myadd(int a, int b);int (int , int )数组-> 指针 -> 再函数[4] ->p[4]->*p[4]->int (*p[4])->int (*p[4])(int , int )这个更简单易解typedef int FUN(int a, int b);FOO *p[] = { myadd ,mysub,mymul,mydive,mymod,mymax };//myadd ,mysub,mymul原创 2021-07-28 19:09:11 · 66 阅读 · 0 评论 -
19_给数值指针起别名—采用三步替换的方式
2、 int a[10] = { 1,2,3 }; int a[10] -> *p 替换掉a -> int (*p)[10] int [10] 数组类型 typedef int ARR[10];//给int [10]取别名为ARR 数组指针,指向的是**整个数组**的地址 对a,进行&,相当于对a进行升级(*a)[10] ********int (*p)[10] = &a;---让p指向整个数组的地址?******** typedef int AR原创 2021-07-28 18:54:35 · 163 阅读 · 0 评论 -
18_函数指针+函数(指针)指针数组_应用
#include<stdio.h>#include<string.h>int myadd(int a, int b){ return a + b;}int mysub(int a, int b){ return a - b;}int mymul(int a, int b){ return a * b;}int mydive(int a, int b){ return a / b;}int mymod(int a, int b){原创 2021-07-28 18:45:08 · 75 阅读 · 0 评论 -
17_typedef_为数组指针,其别名:typedef int (*ARRAY) [10] 更简介一些
int main(){ int a[10] = { 1,2,3 }; //int a[10] ; *p ; int (*p)[10] //int [10] 数组类型 typedef int ARR[10];//给int [10]取别名为ARR ARR *p = &a; printf("%d\n",sizeof(p)); printf("%u %u\n",p,p+1);//ARR * typedef int(*ARRAY)[10];//给 数组指针int(*)原创 2021-07-28 15:09:45 · 615 阅读 · 0 评论 -
16_***重要***计算对p进行的指针、值的前++,后++的计算(!!赋图解!!)
int a[]={1,2,-1,2,3,4,5,6,7,8,9,34,23}' int*p = &a[3]; printf("%d\n",*p++); printf("%d\n",*(p++)); printf("%d\n",(*p)++); printf("%d\n",++(*p)); printf("%d\n",*++p); printf("%d\n",*(++p)); printf("%d\n", *p++); //2 *p p=p+1 printf("%d.原创 2021-07-27 17:55:10 · 70 阅读 · 0 评论 -
15_bug_vs2017_错误列表消失不见
原创 2021-07-25 10:57:57 · 395 阅读 · 0 评论 -
16_注意_printf(“输入的字符串是 :%s\n“, str3);——单独使用,非scanf()时,`必须`对字符串数组中的字符串,添加一个`‘\0‘字符串结束标志`
但还是建议,单独输出字符串内容,否则会输出乱码#include <stdio.h>void printf_array1(char *aaa){ printf("%s\n",aaa);}int main(){ char aaa[8] = "hello"; printf_array1(aaa); return 0;}原创 2021-07-23 16:47:59 · 106 阅读 · 0 评论 -
14_指针的步长(宽度)_应用于指针+1,那么它等于多少呢
3 指针的步长宽度: 取指针变量指向空间的内容 取的字节数步长 :指针变量加1 跨过的字节数宽度== 步长步长 = sizeof(将符号和与符号最近的拿掉剩下的类型)原创 2021-07-23 14:42:46 · 189 阅读 · 0 评论 -
12_bug_无法解析的外部符号 “void __cdecl mswap(int,int)“ (?mswap@@YAXHH@Z),该符号在函数 _main 中被引用
文件名后缀错误原创 2021-07-22 16:30:45 · 320 阅读 · 0 评论 -
11_03days作业_数组方面知识的应用
1、数组元素的总大小等于___ 数组元素的个数乘以元素类型大小______2、不管是几维数组,在物理地址上都是___C__A:多维 B:二维 C:一维 D:由编译器决定3、下面对一维数组int a[5]元素操作错误的是__A___A:a[5]=10; B:printf(“%d\n”, a[2]); C:a[3]=2; D:scanf(“%d”, &a[0]);4、Int a[5]; a代表的是数组的___首元素___的地址5、Int a[5]; &a代表的是数组的_原创 2021-07-22 14:46:43 · 311 阅读 · 3 评论 -
10_拿取一个数组中最大的两个数(不使用排序算法)_对大于、小于、等于的情况做出考虑
//14点17分---拿取最大的两个数#include <stdio.h>int main(){ int a[10] = {4,2,6,555,5259,555,555,4,1,0}; int max1 = 0; int max2 = 0; int i = 0;; int pos = 0; //给max1、max2赋值 if (a[0] > a[1]) { max1 = a[0]; max2 = a[1]; } else if (a[0] <原创 2021-07-22 14:24:17 · 276 阅读 · 0 评论 -
09_第一天练习_案例:求[m,n]之间既不能被7整除也不能被5整除的整数之和,m和n的值由键盘输入
正确代码#include <iostream>int main(){ int a = 0; int b = 0; int i = 0; scanf("%d %d", &a, &b); int left_margin = a > b ? b : a; int right_margin = a > b ? a : b; printf("left = %d\n", left_margin); printf("next = %d\n", right_原创 2021-07-21 10:02:33 · 4560 阅读 · 0 评论 -
08_通过位移运算,将某一位_置‘1’或者置‘0’_对应:打开、关闭
int a = 0x32; 0011 0010 0x01 0000 0001Ⅰ:将第四位置为0 ->(与运算符操作) 0x01 << 4 0001 0000 ~(0x01) : 1110 1111 &a 0011 0010 : 0010 0010 第四位置为0 Ⅱ:将第3位置为1 ->(或运算符操作) 0x01 << 3 : 0000 1000 | a 0011 0010 : 0011 1010..原创 2021-07-20 15:49:48 · 170 阅读 · 0 评论 -
07_不采用第三个变量,交换a\b的值 :(异或方法)
int a = 10; 0000 1010int b = 20; 0001 0100 a = a ^ b = 0001 1110 b = a ^ b = 0000 1010 ->10 a = a ^ b = 0001 0100 ->20原创 2021-07-20 14:54:58 · 101 阅读 · 0 评论 -
06_个人注释版本(01版本)GTK播放器__基于Linux系统下的mplayer播放器
#include <stdio.h>#include "sungtk_interface.h"#include <fcntl.h>#include <sys/types.h>#include <string.h>#include <dirent.h>#define LINE 100#define LIST 128GtkWidget * win;GtkWidget * button_pause;//暂停按键GtkWidget .原创 2021-07-19 20:16:29 · 1380 阅读 · 1 评论 -
05_源码、反码、补码相详细对比计算
int = -129-> 源码 :1000 0000 0000 0000 0000 0000 1000 0001 (取反) 反码 :1111 1111 1111 1111 1111 1111 0111 1110 (+1) 补码 :1111 1111 1111 1111 1111 1111 0111 1111 得数 :-127 char a = 127+2; -> 源码 : 1000 0001 补码 :1000 0001原创 2021-07-19 20:12:37 · 91 阅读 · 0 评论 -
04_GTK播放器-制作过程
GTK播放器-乌班图Linux下自带的mplayer原创 2021-07-19 15:24:08 · 1020 阅读 · 4 评论 -
03_2021-07-14-无命名管道pipe( )的方式创建子进程进行读、写打印
采用无命名管道pipe( )的方式,接着创建一个子进程,去写到fd[1];父进程去读到fd[0]这里不需要open打开文件了,无命名管道的方式,可以直接对管道进行读、写操作,之后直接将管道中的文件直接进行去读打印操作即可但是,子进程还是得创建的,不然就成自己调自己了#include <stdio.h>#include <string.h>#include <unistd.h>int main(){ int fd[2]; pipe(fd); pid_原创 2021-07-14 20:27:39 · 118 阅读 · 0 评论 -
02_创建命名管道7-14
采用mkfifo的方式创建命名管道,用子进程去读,父进程去写pid = fork();//必须去创建一个子进程,否则就会出现,自己写自己读的尴尬情况外层对是否是子、父进程判断,内存对open打印与否进行判断,再接下来,进行读、写操作/* ************************************************************************ * Filename: 01mkfifo.c * Description: *原创 2021-07-14 19:43:19 · 119 阅读 · 0 评论 -
01_open打开\创建一个文件,进行写读操作(7.13)
open#include<stdio.h>#include <unistd.h>#include <fcntl.h> int main(){ int fd = open("./d.txt", O_RDWR | O_CREAT, 0666); if(fd < 0) { perror("open"); } printf("fd = %d\n", fd); close(fd); return 0;}write#include &原创 2021-07-13 19:27:07 · 237 阅读 · 0 评论 -
atexit()函数--接受一个函数指针作为一个参数(调用exit函数,便会执行atexit函数)
#include <stdio.h>#include <stdlib.h>void sign_off(void);void too_bad(void);int main(void){ int n;atexit(sign_off); /* 注册 sign_off()函数 */ puts("Enter an integer:"); if (scanf("%d", &n) != 1) { puts("That's no integer!"); at原创 2021-04-28 21:41:36 · 182 阅读 · 2 评论 -
15章C Primer Plus详细解答--附带详细注释
15.1#include<stdio.h>#include<limits.h>#include<string.h># include <stdlib.h># include <ctype.h>#define SIZE 33int gets_bstr(char *st, int n);bool check_value(const char *st);int binstr_to_dec(const char *st);int ma原创 2021-04-28 21:18:20 · 777 阅读 · 1 评论 -
递归三要素--带例题
递归三要素一、函数所要实现的功能二、终止的条件三、找出与函数等价的关系式(意思是:同样功能实现)例题C primer plus的第九章-9题,采用递归实现对整数次幂的计算,在另外一个文章中,有验算记录...原创 2021-02-06 09:26:56 · 262 阅读 · 0 评论 -
C Primer Plus 第六版(中文版)第十三章(完美版---》加个人注释)编程练习答案
13.1#include <stdio.h>#include <stdlib.h>#define LEN 50int main(int argc, char *argv[]){ int ch; FILE *fp; unsigned long count = 0; char filename[LEN]; printf("Please enter a filename:\n"); scanf("%30s", filename)原创 2021-01-18 09:55:41 · 345 阅读 · 0 评论