文章

c 语言常用库函数示例

c 语言常用库函数示例

relloc

  • 头文件: #include <stdlib.h>
  • 介绍: 内存重分配
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main() {
   char *p;
   p = (char *) malloc(10);
   strcpy(p, "hello");
   printf("Str = %s,  Addr = %p\n", p, p);
   p = (char *) realloc(p, 20);
   strcat(p, " world");
   printf("Str = %s,  Addr = %p\n", p, p);
   free(p);
   return(0);
}

strchar

  • 头文件: #include <string.h>
  • 介绍: 常用于查找字符串中指定字符的位置 下面是一个查找 '*' 位置的示例: ```c++ #include #include

int main(void) { char a[] = “helloworld”; char b[25] = { 0 }; char c[25] = { 0 }; char *p = strchr(a, ‘’); printf(“%p\n”, a); printf(“%p\n”, p); strncpy(b, a, p - a); // p 的地址减去 a 的地址等于字符串地址差 strcat(b, “\n\0”); strncpy(c, p + 1, strlen(p + 1)); strcat(c, “\n\0”); printf(“%s”, (char *)b); printf(“%s”, (char *)c); return 0; }

1
2
3
4
5
6
```bash
➜  ~ ./a.out 
0x7ffd5acf92b4
0x7ffd5acf92b9
hello
world

strtok

  • 头文件: #include <string.h>
  • 介绍: 字符串分割的函数 下面是一个分割字符串的示例:
1
// TODO
1

strcat

##

本文由作者按照 CC BY 4.0 进行授权

© xiongyi. 保留部分权利。

|

岂堪久蔽苍苍色,须放三光照九州