阅读源码是对自己代码质量提升最好的方法
源码 https://download.youkuaiyun.com/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<stdio.h>
#include<stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include<string.h>
#include<errno.h>
#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)$<:代表第一个依赖文件。
## get all include path
CFLAGS += $(foreach dir, $(INCLUDE_PATH), -I$(dir))
.PHONY: all #伪所址 不管有没有all 都会执行
$(TARGET):
@echo $(SRCS)
$(CC) -c $(CFLAGS) $(SRCS)
$(CC) $(CFLAGS) -o $(TARGET) $(OBJS) # $(LDFLAGS)
$(RM) $(OBJS)
clean: #清除文件
$(RM) *.o $(TARGET)