当前位置:网站首页>C language improvement (3)
C language improvement (3)
2022-08-02 13:43:00 【Tandy12356_】
实现字符串拷贝函数:myStrcpy
void myStrcpy(char* dst, const char* src) {
int len = strlen(src);
for (int i = 0; i < len; ++i) {
dst[i] = src[i];
}
dst[len] = '\0';
}
void test() {
const char* src = "hello world!";
char buffer[1024] = { 0 };
myStrcpy(buffer, src);
printf("%s\n", buffer);
}
1、It is best to use it when initializingbuffer置零,即:const char* src = "hello world!";
2、Remember to add one at the end after copying the strings one by one'\0'
方法2:
//Final judgment if*dst的值是0的话,程序就退出了
void myStrcpy03(char* dst, const char* src) {
while (*dst++ = *src++);
}
while (*dst++ = *src++);
00C51D65 mov eax,dword ptr [dst]
00C51D68 mov ecx,dword ptr [src]
00C51D6B mov dl,byte ptr [ecx]
00C51D6D mov byte ptr [eax],dl
00C51D6F mov eax,dword ptr [dst]
00C51D72 mov cl,byte ptr [eax]
00C51D74 mov byte ptr [ebp-0C1h],cl
00C51D7A mov edx,dword ptr [dst]
00C51D7D add edx,1
00C51D80 mov dword ptr [dst],edx
00C51D83 mov eax,dword ptr [src]
00C51D86 add eax,1
00C51D89 mov dword ptr [src],eax
00C51D8C movsx ecx,byte ptr [ebp-0C1h]
00C51D93 test ecx,ecx
00C51D95 je std::_Narrow_char_traits<char,int>::eof+19h (0C51D99h)
00C51D97 jmp __$EncStackInitStart+19h (0C51D65h)
TestThe command logically ANDs two operands,并根据运算结果设置相关的标志位.但是,TestThe two operands of the command不会被改变.The result of the operation will be discarded after the relevant flag bit is set.
TEST AX,BX
与AND AX,BX
command has the same effect,只是TestInstructions do not changeAX和BX的内容,而ANDThe command will save the result to AX中.
1、*dst=*src
2、dst++,src++
3、判断++之前的*dst是否为0,如果为0就跳出循环,Jump to if not zero1去执行
前置++和后置++的区别:
y=x++;
00DE1BF1 mov eax,dword ptr [x]
00DE1BF7 mov dword ptr [y],eax
00DE1BFD mov ecx,dword ptr [x]
00DE1C03 add ecx,1
00DE1C06 mov dword ptr [x],ecx
y = ++x;
00DE1C0C mov eax,dword ptr [x]
00DE1C12 add eax,1
00DE1C15 mov dword ptr [x],eax
00DE1C1B mov ecx,dword ptr [x]
00DE1C21 mov dword ptr [y],ecx
但是对于i++/++iThis addition does not affect the result
for循环的执行流程:
for (int i = 0; i < 5; ++i) {
00DE1BCD mov dword ptr [ebp-438h],0
00DE1BD7 jmp allocSpace+8h (0DE1BE8h)
00DE1BD9 mov eax,dword ptr [ebp-438h]
00DE1BDF add eax,1
00DE1BE2 mov dword ptr [ebp-438h],eax
00DE1BE8 cmp dword ptr [ebp-438h],5
00DE1BEF jge __vfprintf_l+9h (0DE1C29h)
字符串反转:
写入访问权限冲突:If it is a lower address it is possible to use a null pointer,If it is a different address,It is possible that wild pointers are used or access rights are not available(For example, the constant area)
注意:形如char*str=(char*)helloSuch string constants are stored in the constant area,Him and the worldconst一样,Once initialized, it cannot be modified.(It also cannot be modified through pointers)
实现字符串反转——指针型
void myStrReverse(char* src) {
if(NULL==src){
return;
}
int len = strlen(src);
char tmp = 0;
char* pHead = src;
char* pTail = src + (len - 1);
while (pHead < pTail) {
tmp = *pHead;
*pHead= *pTail;
*pTail = tmp;
--pTail;
++pHead;
}
}
实现字符串反转——subscript
void myStrReverse2(char* src) {
if(NULL==src){
return;
}
int len = strlen(src);
char tmp = 0;
int start = 0;
int end = len - 1;
while (start < end) {
tmp = src[start];
src[start] = src[end];
src[end] = tmp;
++start;
--end;
}
}
When a function uses a formal parameter pointer, you must first judge!!!
格式化字符串——sprintf
The buffer we often refer to is a blockbuffer,也就是一块内存
printfIt is to put the formatted string into the display,而sprintfis to put the formatted string inbuffer里面了
sprintf的三大作用:
1、格式化字符串
2、拼接字符串
3、将数字转化成字符串
void test() {
//1.格式化字符串
char buffer[1024] = { 0 };
sprintf(buffer, "hello %s", "world");
printf("%s\n", buffer);
//2.拼接字符串
const char* str1 = "hello";
const char* str2 = "Obama";
memset(buffer, 0, 1024);
sprintf(buffer, "%s %s", str1, str2);
printf("%s\n", buffer);
//3.Convert numbers to string format
int num = 666;
memset(buffer, 0, 1024);
sprintf(buffer, "老铁双击%d\n", num);
printf("%s\n", buffer);
}
void test() {
char** p = (char**)malloc(sizeof(char*) * 5);
if (NULL == p) {
return;
}
memset(p, 0, sizeof(char*) * 5);
for (int i = 0; i < 5; ++i) {
p[i] = (char*)malloc(64);
memset(p[i], 0, 64);
sprintf(p[i], "string%d", i + 1);
}
for (int i = 0; i < 5; ++i) {
printf("%s\n", p[i]);
}
for (int i = 0; i < 5; ++i) {
if (p[i] != NULL) {
free(p[i]);
p[i] = NULL;
}
}
if (p != NULL) {
free(p);
p = NULL;
}
}
Do some judgment before freeing heap memory!!!
calloc和realloc:(仅供了解)
骚操作:按住alt键,Drag the mouse to select down,可以打印多行
Find a string:
const char* myStrStr(const char* str, const char* subStr) {
const char* myStr = str;
const char* mySub = subStr;
while (*myStr!='\0')
{
if (*myStr != *subStr) {
++myStr;
continue;
}
const char* tmp = myStr;
while (*mySub != '\0') {
if (*myStr != *mySub) {
mySub = subStr;
break;
}
++mySub;
++myStr;
}
if (*mySub == '\0') {
return tmp;
}
++myStr;
}
return NULL;
}
void test() {
const char* str = "abcdefg";
const char* subStr = "gh";
const char*sub=myStrStr(str, subStr);
printf("%s\n", sub);
}
Pointers are prone to errors:
Move the value of a pointer to heap space,使得无法freeremove this memory
void test() {
char* p = (char*)malloc(100);
p++;
free(p);
p = NULL;
}
发现调用freefunction crashes,This is the case because we moved the pointermalloc地址的指针!
When you operate a certain piece of memory, make sure that it is legal(自己申请的,Also not released)
So it must not return the address of a local variable!!!!!!
所以freeBe sure to set the pointer after it is droppedNULL
边栏推荐
猜你喜欢
【C语言】手撕循环结构 —— for语句
【622. 设计循环队列】
Automatically generate code generator recommendation-code-gen
RESTful 风格(详细介绍 + 案例实现)
Embedded system driver primary [2] - based on character device driver _ basic framework
MySQL - ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
How to create short images and short videos from the media?How to make the click volume reach 10W?
电脑死机,Word忘了保存怎么办?怎么恢复?(编辑器是WPS)
【ONE·Data || 排序入门】
劲爆!阿里巴巴面试参考指南(嵩山版)开源分享,程序员面试必刷
随机推荐
方正璞华“劳动人事法律自助咨询服务平台”在武汉武昌区投入使用!
Introduction to Scala Basic Syntax (3) Various Operators in Scala
The uniapp/applet onload method executes the interpretation every time the page is opened
大而全的pom文件示例
短视频美食自媒体怎么做?5步教你快速上手
矩阵中的路径
Seata分布式事务
[C language] Analysis of function recursion (2)
How to connect DBeaver TDengine?
wait() ,notify(),notifyAll()以及wait()与sleep()比较
永远退出机器学习界!
Cannot determine loading status from target frame detached when selenium chrome driver is running
requestparam注解接的收的是什么格式(玄机赋注解)
wx-wow(微信小程序动效库)
SQL函数 USER
RESTful 风格(详细介绍 + 案例实现)
Singleton pattern of seven kinds of writing, you know?
80篇国产数据库实操文档汇总(含TiDB、达梦、openGauss等)
uniapp/小程序 onload方法每次打开页面都执行解读
How to create short images and short videos from the media?How to make the click volume reach 10W?