当前位置:网站首页>[advanced C language] string and memory function (I)
[advanced C language] string and memory function (I)
2022-06-30 00:10:00 【Haozai lives today】
List of articles
1、 Unlimited length string function
1.1、strlen function
size_t strlen(const char* str);
1、sizeof — The operator — Calculate the size of ,sizeof The type returned is size_t, and size_t Essentially, unsigned int type
// Simulation Implementation strlen
#include <stdio.h>
#include <string.h>
#include <assert.h>
int my_strlen(const char* str)
{
assert(str);// Assertion str It's not a null pointer
int count = 0;
while (*str != '\0')
{
count++;
str++;
}
return count;
}
int main()
{
int len = my_strlen("abcdef");
printf("%d\n", len);
return 0;
}
2、 String to ‘\0’ As an end sign ,strlen Function returns in a string ‘\0’ The number of characters that appear before , It doesn't contain ’\0’
3、 The string pointed to by the parameter must be in ‘\0’ end
1.2、strcpy function
1、 The source string must be in ‘\0’ end ( The source string is the copied string )
2、strcpy The function will the... In the source string ‘\0’ Copy to target space
3、 The target space has to be large enough , To ensure that the source string can be stored
4、 The target space has to be variable ( Rebuttal )
// Wrong example , Code can't be written like this !!!
#include <stdio.h>
#include <string.h>
int main()
{
char arr1[] = "abcdef";
const char* p = "xxxxxxxx";
// At this time, the pointer variable p By const modification ,p The content of the point cannot be changed , So it's wrong
strcpy(p, arr1);
printf("%s\n", p);
return 0;
}
5、 Simulation Implementation strcpy function
#include <assert.h>
#include <stdio.h>
// Simulation Implementation strcpy function
char* my_strcpy(char* dest,const char* src)
{
char* ret = dest;
assert(dest && src);// Assert that neither is a null pointer
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 function
1、 Append the contents of the string to Destination string Back
2、 The target space has to be large enough , It can hold the contents of the source string
3、 The appended string must be followed by ’\0’
4、 The target string must be followed by ’\0’
5、 The target space must be modifiable
6、 Simulation Implementation strcat function 
1.4、strcmp function
1、 String comparison ( Compare the character size at the corresponding position )
2、 The comparison is the corresponding character position ASCII Code value
3、 Simulation Implementation strcmp function 
2、 String function with limited length
2.1、strncpy function
Examples of use
#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 function
Use cases
int main()
{
char arr1[20] = "hello";
char arr2[] = "world";
strncat(arr1, arr2, 5);
printf("%s\n", arr1);
return 0;
}
2.3、strncmp function
Use cases
int main()
{
char arr1[] = "abcdef";
char arr2[] = "abcqqqqq";
int ret = strncmp(arr1, arr2, 4);
printf("%d\n", ret);
return 0;
}
3、strstr function
1、 The function is to find the target string in the string , Here are the use cases
#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(" Did not find \n");
}
else
{
printf("%s\n", ret);
}
return 0;
}
2、 Simulation Implementation strstr function 


Little knowledge
1、 When the pointer doesn't know what value to assign , Just give it to NULL
2、 After the pointer is used , assignment NULL
3、 Chained access ( give an example )
4、 Two strings cannot be compared directly with an equal sign , It can't be subtracted , Because when using two strings , What is produced is the first address of two strings
int main()
{
int len = strlen("abcdef");
printf("%d\n",strlen("abcdef"));
return 0;
}
Wrong example
// Error code , Do not imitate
int* test()
{
int a = 10;
return &a;
}
int main()
{
int* p = test();
*p = 20;// Form illegal access
// Because the space requested by the temporary variable in the function
// It's destroyed when it comes out of the function
// Therefore, illegal access will be formed
return 0;
}
边栏推荐
- Cloner un Graphe non recté [bfs accède à chaque bord et pas seulement aux noeuds]
- Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus
- gyctf_2020_document
- Halcon practical: design idea of solder joint detection
- Buffer flow exercise
- Embedded development: Hardware in the loop testing
- Mysql:sql overview and database system introduction | dark horse programmer
- How to view the CPU cores and threads in win11? Win11 view the tutorial of how many cores and threads the CPU is
- Code analysis platform sonarqube actual combat
- solo 博客皮肤导入 skins 文件夹后出现 500 错误
猜你喜欢

Leetcode (76) -- Minimum Covering substring

Golang6 reflection

The role of VMware virtual machine

基于zfoo开发项目的一些规范

QT learning 07 coordinate system in QT

Siemens low code platform connects MySQL through database connector to realize addition, deletion, modification and query

Construction of module 5 of actual combat Battalion
![[rust weekly library] Tokei - a utility for statistics of code lines and other information](/img/6c/4569cc0edaa01e4605c9c256193c31.png)
[rust weekly library] Tokei - a utility for statistics of code lines and other information

MySQL multi table query

QT learning 05 introduction to QT creator project
随机推荐
Table responsive layout tips for super nice
JVM之栈空间
克隆无向图[bfs访问每条边而不止节点]
Cartoon security HIDS, EDR, NDR, XDR
label问题排查:打不开标注好的图像
MySQL multi table query
Virtual machine online migration based on openstack
除子
由GlideException: Failed DecodePath{DirectByteBuffer->GifDrawable->Drawable}引起的刨根问底
QT learning 03 birth and essence of QT
modelsim的TCL脚本的define incdir命令解析
After 8 years of polishing, "dream factory of game design" released an epic update!
How about counting Berry Pie 4? What are the possible ways to play?
Construction of module 5 of actual combat Battalion
Web APIs 环境对象 丨黑马程序员
Inspiration collection · evaluation of creative writing software: flomo, obsidian memo, napkin, flowus
golang7_ TCP programming
Solr基础操作12
This simple little function saves 213 hours for our production research team in half a year
Sword finger offer 14- I. cut rope