www.zhblog.net

C指针数组

C指针数组#include <stdio.h> #define AMAX 4 int main() { char *name[AMAX] = {"Illegal month", "Jan", "Feb", "Mar"}; char **p = name; for (int i = 0; i < AMAX; ++i) { printf("%s\n", *p); p++; } }Illegal month......

字符串\n、\0以及保留字符串起始位置

int main() { char *s = malloc(16); char *p = s; *s++ = 'a'; *s++ = 'c'; *s++ = '\n'; *s++ = '\0'; printf("result: %s", p); }*p保留字符串起始位置。因为s++会将指针后移,以至于获取s值为'\0'。'\n'是换行作用,printf时可以自动换行。'\0'是字符串结束符。......

数组与指针互换

int main() { char *name[AMAX] = {"Illegal month", "Jan", "Feb", "Mar"}; printf("%c\n", **name); printf("%c\n", (*name)[0]); }结果一致:I   ......

函数指针

#include <stdio.h> #include <string.h> void cmp(void *s, void *t, int (*comp)(void *, void *)); void cmp2(void *days[], int (*comp)(void *, void *)); int main() { char *s = "mnc"; char *t = "abc"; cmp((void *) s, (void ......

格式化字符串

函数sprintf执行的转换和函数printf相同,它将结果保存到一个字符串中:#include <stdio.h> int main() { char *s = "python"; char ns[32]; sprintf(ns, "language = %15.10s", s); printf("%s\n", ns); }......

内存分配与野指针

用一个闭环式链表管理内存分配。1.头块是特殊块,下一个块指向自己本身,size = 0。2.内存不足则向系统申请内存,并添加到链表中。#include <stdio.h> #include <unistd.h> #define MIN_MALLOC 1024 union header { struct { union header *next; int size; } s; long Align; };......

C编码转换

c语言字符集转换库:libiconv1.将字符串由UTF8转为GBK#include <stdio.h>#include <iconv.h>#define ARRAY_SIZE 16int main() { const char *fromcode = "UTF8"; const char *tocode = "GBK"; iconv_t it = iconv_open(tocode, fromcode); char ins[ARRAY_SIZE] = "中国......

CMake添加第三方库

在Clion CMake中添加第三方依赖库cmake_minimum_required(VERSION 3.14)project(untitled C)set(CMAKE_C_STANDARD 99)set(INC_DIR /usr/local/include/)set(LINK_DIR /usr/local/lib)include_directories(${INC_DIR}) # 头文件目录link_directories(${LINK_DIR}) # 库目录link_libraries(pcre)......

C语言正则表达式

1.POSIX标准库中正则表达式#include <stdio.h>#include <regex.h>#include <string.h>#include <stdlib.h>int main() { regex_t rt; const char *pattern = ".*/([a-z]+)/(\\w+.html)$"; const char *str = "/home/blog/index.html"; const int gr......

获取范围内的随机整数

#include <stdlib.h>#include <time.h>#include <stdio.h>int getrand(int, int);int main() { for (int i = 0; i < 10; ++i) { printf("%d\t", getrand(10, 200)); } return 0;}int getrand(int begin, int end) { int div = end - ......
1 2 3 ... 7