前期提醒:这是比特鹏哥上课讲的内容,鹏哥讲的非常好,大家可以了解一下。
先来讲一讲strtok函数:
int main() { const char* p = "@.#"; char arr[] = "zpengwei@yeah.net#hehe"; char buf[50] = { 0 };// "zpengwei@yeah.net" strcpy(buf, arr); char* str = NULL; for (str = strtok(buf, p); str != NULL; str=strtok(NULL, p)) { printf("%sn", str); }//运用循环来实现 char* str = strtok(buf, p);//zpengwei printf("%sn", str); str = strtok(NULL, p);//yeah printf("%sn", str); str = strtok(NULL, p);//net printf("%sn", str); strtok - 开始返回NULL return 0; }实现memove和memcpy函数:
memcpy函数挺简单的,主要还是说一下memmove函数:
#include#include #include #include #include void* my_memmove(void* dest,const void* str,size_t num) { void* ret = dest; if (dest < str)//前->后 { while (num--) { *(char*) dest = *(char*)str; dest = (char*)dest + 1; str = (char*)str + 1; } } else//后->前 { dest = (char*)dest + num-1; str = (char*)str + num-1; while (num--) { *(char*)dest = *(char*)str; dest = (char*)dest - 1; str = (char*)str - 1; } } return ret; } void* my_memcpy(void* dest, const void* str, size_t num) { void* ret = dest; assert(dest && str); while (num--) { *(char*)dest = *(char*)str; dest = (char*)dest + 1; str = (char*)str + 1; } return ret; } int main(void) { int a[] = { 1,2,3,4,5,6,7,8,9,10 }; int b[10] = { 0 }; my_memcpy(b,a,5*sizeof(int)); int i = 0; for (i = 0; i < 5; i++) { printf("%d ", b[i]); } return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)