当前位置:网站首页>[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;
}
边栏推荐
- Solr basic operation 6
- Solr基础操作9
- Zhongkang holdings opens the offering: it plans to raise HK $395million net, and it is expected to be listed on July 12
- Color space conversion in video tonemapping (HDR to SDR) (bt2020 to bt709, YCbCr, YUV and RGB)
- golang7_ TCP programming
- Solr basic operations 9
- Solr基础操作15
- Et la tarte aux framboises 4? Quels sont les jeux possibles?
- Solr basic operations 12
- 8软件工程环境
猜你喜欢

Color space conversion in video tonemapping (HDR to SDR) (bt2020 to bt709, YCbCr, YUV and RGB)

Leetcode (76) -- Minimum Covering substring

數莓派 4怎麼樣?可能的玩法有哪些?

Matlab exercises -- program control process exercise

This simple little function saves 213 hours for our production research team in half a year

Vulnhub target -moriartycorp

Cloud native enthusiast weekly: cool collection of grafana monitoring panels

AI chief architect 9- huxiaoguang, propeller model library and industry application

Some specifications based on zfoo development project

Applet plug-in access, development and precautions
随机推荐
JS绘制极坐标颜色渐变
Solr basic operations 9
Solr基础操作15
JVM之栈空间
EB-5 immigration in the United States reappears to be positive, and the reauthorization policy of the regional center is suspended
Golang6 reflection
Solr基础操作6
QT learning 05 introduction to QT creator project
modelsim的TCL脚本的define incdir命令解析
Label Troubleshooting: unable to open the marked image
[rust weekly library] Tokei - a utility for statistics of code lines and other information
QT learning 06 widgets and window types
Mysql:sql overview and database system introduction | dark horse programmer
Which securities company is good for opening a mobile account? In addition, is it safe to open a mobile account?
ThinkPad VMware installation virtual machine: this host supports Intel VT-x, but Intel VT-x is disabled (problem resolution)
Ingenious application of golang generics to prevent null pointer errors of variables and structural fields
Vulnhub target -moriartycorp
How to view the CPU cores and threads in win11? Win11 view the tutorial of how many cores and threads the CPU is
项目一:部署 LAMP ecshop电商平台
HTAP x cloud native: tidb accelerates the release of data value and realizes data agility