实现自己的strcpy函数,初次写:
char * mystrcpy(char* dst,char* src)
{
while(*src!='\0') //规定src小于dst
{
*dst++=*src++;
}
return dst;;
}
错误:返回的dts指针是已经移动位置的指针
没有在dst最后加上字符串的结束符,
改进后
附上:C语言中,为什么字符串可以赋值给字符指针变量
https://www.cnblogs.com/skullboyer/p/7807543.html
har * mystrcpy(char* dst,char* src)
{
char* temp=dst; //保留dst的首地址
while(*src!='\0') //规定src小于dst
{
*dst++=*src++;
}
*dst='\0'; //结束符
return temp;;
}
设计一个函数getmemory申请100个字节的内存并将该内存传递回来。
第一次写
#include <stdio.h>
char* getmemory() {
char fstr[100];
char* str;
str = (char*)malloc(sizeof(fstr));
return str;
}
缺点:没必要再定义一个str数组
改进后
void gettmemory(char ** str)
{
*str=(char*)malloc(100);
}
char* gettmemory()
{
char* str=(char*)malloc(100);
return str;
}