目录
my_strcmp函数 my_strcpy函数 my_strcat函数
小知识点总结
my_strcmp函数
int my_strcmp(const char* str,const char* src) {
assert(str!=NULL && src!=NULL);
while(*str==*src){
if(*src=='\0') break;
str++;
src++;
}
if(*str=='\0'&&*src=='\0') return 0;
return *str>*src ?1:-1;
}
库函数中strcmp的调用也是以'\0'为结尾标记。
my_strcpy函数
void my_strcpy(char*str,const char*src){
assert(str!=NULL &&src !=NULL);
while(*src!='\0'){
*str++=*src++;
}
*str='\0';
}
库函数中strcpy的调用也是以'\0'为结尾标记。
my_strcat函数
void my_strcat(char*str,const char*src){
while (*str++!='\0');//str指向'\0'
while(*src!='\0'){
*str++=*src++;
}
*str='\0';
}
库函数中strcat的调用也是以'\0'为结尾标记。
杂乱知识点总结
int main() {
const char* str="hello";//.data
const char* src="hello";
const char* s="world";
const char* d="world";
printf("%d\n",str==src);
}
字符串常量 存储位置在数据区(全局变量,。data)
str首先在常量池中查找是否有"hello",若不存在 会在常量池中申请内存存放"hello";运行src时便可以在常量池中找到"hello",找到字符串存储的地址,将地址给src,所以src==str 为真(true)。
int main() {
char arr[]="hello";
char brr[]="hello";
}
这种情况下 arr[ ]的建立,在main栈申请一个连续空间存放hello\0,brr[ ]建立时仍在main栈中申请一个连续空间存放hello\0,字符数组形式 在局部作用域 main栈中申请内容,一个数组 一份 " ".此时arr=brr 为错误(false).
如果企图通过空指针来访问一个存储单元,将会得到一个出错信息
char* s ;int *p; s+1*sizeof(char); p+1*sizeof(int);
#define NULL (void*) 0,int a=NULL; NULL (void*)0 '\0' 三者均为0000 0000,
double *p;
p++;
p+1;
p++:将指针p移动了8个字节(因为double占8字节)
p+1:偏移量偏移了8个字节,指针本身未发生改变。
定义一个变量若没有赋值,全局变量默认赋值为0 局部变量默认为随机值。
指针所占内存大小只与操作系统有关,与类型名无关,x86 4字节 x64 8字节
int main() {
int arr[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
int *p[4]=arr;
}
在上述代码的前提下,arr[0][3]<=>*(*(p+0)+3)<=>*(*(p)+3)<=>(*p)[3]等价由该等价公式 arr[3]=*(arr+3);推理得出