阅读源码之 |
您所在的位置:网站首页 › strcpy和strcat › 阅读源码之 |
阅读源码是对自己代码质量提升最好的方法 源码 https://download.csdn.net/download/weixin_39328406/88312874?spm=1001.2014.3001.5501 char strcpy(chardst, const charsrc)* dst :destination的缩写 src:source的缩写 strcpy : string copy char* strcpy(char*dst, const char *src) { if ((dst == NULL) || (src == NULL)) return NULL; char *ret = dst; //[1] while ((*dst ++= *src++) != '\0'); //[2] return ret; //[3] }const :防止在赋值的给dst的时候发生改变; ret :return ,返回的是dst的首地址。 while ((*dst ++= *src++) != ‘\0’); 当 *src =‘\0’ 的时候为false while()结束。 *dst ++= *src++的时序问题 首先:*dst= *src 然后:dst++和src++顺序不定,不同的编译器可能顺序不同 main.c 的源码 #include #include #include #include #include #include #define N 50 char* strcpy(char*dst, const char *src) { if ((dst == NULL) || (src == NULL)) return NULL; char *ret = dst; //[1] while ((*dst ++= *src++) != '\0'); //[2] return ret; //[1] } int main( ) { char str1[N]; char *str2 = "hello world ...... ...... "; printf("%s\n", strcpy(str1, str2)); printf("%s\n", str1); printf("%s\n", str2); return 0; } makefile文件 ```c #20181220 CC = gcc #编译器的型号 RM = rm -rf #删除命令 TARGET := strcpy #目标文件 SRC_PATH := . SRCS = $(wildcard $(SRC_PATH)/*.c ) #它的功能是找到目标文件下的.c文件并将它们全都赋给SRCS。 OBJS := $(SRCS:.c=.o) #它的功能是把后缀中的.c 文件替换为 .o文件并赋给OBJS。 #OBJS=$(patsubst %.c , %.o , $(SRCS)) #函数它的功能是把后缀中的.c 文件替换为 .o文件并赋给OBJS。 ## used headers file path INCLUDE_PATH := . #3)三个变量 #1)$@:代表目标文件。 #2)$^:代表所有的依赖文件。 #3)$ |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |