当前位置:网站首页>【C语言进阶】字符串和内存函数(一)
【C语言进阶】字符串和内存函数(一)
2022-06-29 09:36:00 【皓仔活在今天】
文章目录
1、长度不受限制的字符串函数
1.1、strlen函数
size_t strlen(const char* str);
1、sizeof — 操作符 — 计算大小的,sizeof返回的类型是size_t,而size_t本质上是unsigned int类型
//模拟实现strlen
#include <stdio.h>
#include <string.h>
#include <assert.h>
int my_strlen(const char* str)
{
assert(str);//断言str不是空指针
int count = 0;
while (*str != '\0')
{
count++;
str++;
}
return count;
}
int main()
{
int len = my_strlen("abcdef");
printf("%d\n", len);
return 0;
}
2、字符串以 ‘\0’ 作为结束标志,strlen函数返回的是在字符串中 ‘\0’前面出现的字符个数,不包含’\0’
3、参数指向的字符串必须以 ‘\0’ 结束
1.2、strcpy函数
1、源字符串必须以 ‘\0’ 结束(源字符串即被拷贝的字符串)
2、strcpy函数会将源字符串中的 ‘\0’ 拷贝到目标空间
3、目标空间必须足够大,以确保能存放源字符串
4、目标空间必须可变(举反例)
//错例,代码不能这样写!!!
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[] = "abcdef";
const char* p = "xxxxxxxx";
//此时指针变量p被const修饰,p指向的内容不能改变,所以是错的
strcpy(p, arr1);
printf("%s\n", p);
return 0;
}
5、模拟实现strcpy函数
#include <assert.h>
#include <stdio.h>
//模拟实现strcpy函数
char* my_strcpy(char* dest,const char* src)
{
char* ret = dest;
assert(dest && src);//断言两个都不为空指针
while (*dest++ = *src++)
{
;
}
return ret;
}
int main()
{
char arr1[] = {
'a','b','c','d','e','f','\0' };
char arr2[20] = "xxxxxxxxxx";
my_strcpy(arr2, arr1);
printf("%s\n", arr2);
return 0;
}
1.3、strcat函数
1、将字符串中的内容追加到 目的字符串 后面
2、目标空间必须足够大,能容纳下源字符串的内容
3、追加的字符串后必须要有’\0’
4、目标字符串后必须要有’\0’
5、目标空间必须可修改
6、模拟实现strcat函数
1.4、strcmp函数
1、字符串比较(比较的是对应位置上的字符大小)
2、比较的是对应字符位置的ASCII码值
3、模拟实现strcmp函数
2、长度受限制的字符串函数
2.1、strncpy函数
使用例子
#include <stdio.h>
int main()
{
char arr1[] = "xxxxxxxxxxxxxxxx";
char arr2[] = "hello world";
strncpy(arr1, arr2, 5);
printf("%s\n", arr1);
return 0;
}
2.2、strncat函数
用例
int main()
{
char arr1[20] = "hello";
char arr2[] = "world";
strncat(arr1, arr2, 5);
printf("%s\n", arr1);
return 0;
}
2.3、strncmp函数
用例
int main()
{
char arr1[] = "abcdef";
char arr2[] = "abcqqqqq";
int ret = strncmp(arr1, arr2, 4);
printf("%d\n", ret);
return 0;
}
3、strstr函数
1、作用是在字符串中查找目标字符串,下面是用例
#include <assert.h>
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[] = "abcdefabcdef";
char arr2[] = "bcd";
char* ret = strstr(arr1, arr2);
if (NULL == ret)
{
printf("没找到\n");
}
else
{
printf("%s\n", ret);
}
return 0;
}
2、模拟实现strstr函数


小知识
1、指针不知道赋什么值的时候,就给NULL
2、指针使用完后,赋值NULL
3、链式访问(举例)
4、两个字符串不能直接用等号比较,也不能相减,因为使用两个字符串时,产生的是两个字符串的首地址
int main()
{
int len = strlen("abcdef");
printf("%d\n",strlen("abcdef"));
return 0;
}
错例
//错误代码,请勿模仿
int* test()
{
int a = 10;
return &a;
}
int main()
{
int* p = test();
*p = 20;//形成非法访问
//因为函数中的临时变量申请的空间
//在出函数的时候就被销毁了
//所以会形成非法访问
return 0;
}
边栏推荐
猜你喜欢

AQS之Atomic详解

Analysis of BlockingQueue source code of AQS

你的项目需要自动化测试吗?

Software test model (V model and W model)

《CLR via C#》读书笔记-加载与AppDomain

Common usage of LINQ in C #

WinForm uses zxing to generate QR code

C#窗体向另一个窗体实时传值

UserWarning: Usage of dash-separated ‘script-dir‘ will not be supported in future versions. 笔记

Fully understand the MESI cache consistency protocol
随机推荐
September 25, 2020 noncopyable of boost library for singleton mode
Automatic 3D Detection and Segmentation of Head and Neck Cancer from MRI Data.
How to quickly complete disk partitioning
How can I get the stock account opening discount? Also, is it safe to open an account online?
winform使用zxing生成二维码
Virtual machine port scanning
BUUCTF--reverse2
ssh密钥泄露(B模块赛题)——应用服务漏洞扫描与利用
Findbugs修改总结
全面理解MESI缓存一致性协议
F5 big IP Icontrol rest command execution (cve-2022-1388)
Web vulnerability manual detection and analysis
Oracle重置序列发生器(非重建)
Add/modify/drop column of alter table operation in MySQL
Offensive and defensive world re insfsay
Analysis of liferayportal jsonws deserialization vulnerability (cve-2020-7961)
Graphics learned from jigsaw puzzles h
《CLR via C#》读书笔记-加载与AppDomain
Buuctf-- connotative software
Ora-01950 does not have permission on tablespace