传智播客_C提高讲义 下载本文

内容发布更新时间 : 2024/5/11 3:28:54星期一 下面是文章的全部内容请认真阅读。

欢迎阅读 while( (*to++=*from++) !='\\0') { ; } } int copy_str05_good(const char *from, char *to) { if (from==NULL || to==NULL) { printf(\ return -1; } while( (*to++=*from++) !='\\0') { ; } return 0; } 典型错误知多少 char *str_cnct(char *x, char* y) /*简化算法*/ { char str3[80]; char *z=str3; /*指针z指向数组str3*/ while(*z++=*x++); z--; /*去掉串尾结束标志*/ while(*z++=*y++); z=str3; /*将str3地址赋给指针变量z*/ return(z); } 修改字符常量结果会如何 Char *p = “abcdefg”; Modify p[1] = ‘1’; 04字符串操作易错 //你往哪里输入数据 int main() { char buf[2000]; char *p = NULL; p = buf; printf(\请输入一个字符串:\ scanf(\ printf(\ getchar(); getchar(); return 0; 欢迎阅读

欢迎阅读 } 3.3库函数api 快速的上手api是一种能力! 建立正确的程序运行示意图,(内存四区及函数调用堆栈图)是根本保障!! int main31() { char buf1[100]; char buf2[200]; strcpy(buf1, \ printf(\ getchar(); return 0; } int main32() { char *string2 = \ int length; //在字符str1中查找,与str2中任意字符有公共交集的位置 length = strcspn(string1, string2); printf(\ getchar(); return 0; } //strnset函数有错误 //测试程序修改如下 int main33() { char string[] = \ char letter = 'x'; printf(\ strnset(string, letter, 13); printf(\ strnset: %s\\n\ getchar(); return 0; } int main44() { char *string1 = \ char *string2 = \ char *ptr; ptr = strpbrk(string1, string2); if (ptr) printf(\ else 欢迎阅读

欢迎阅读 printf(\ getchar(); return 0; } int main55() { char input[16] = \ char *p; /* strtok places a NULL terminator in front of the token, if found */ p = strtok(input, \ if (p) printf(\ /* A second call to strtok using a NULL as the first parameter returns a pointer to the character following the token */ p = strtok(NULL, \ if (p) printf(\ getchar(); return 0; } //典型的状态函数 int main() { char str[] = \country\ //char delims[] = \ char *delims = \ char *result = NULL; result = strtok( str, delims ); while( result != NULL ) { printf( \ result = strtok( NULL, delims ); } printf(\ printf(\ getchar(); return 0; } 3.4字符串相关一级指针内存模型 void main() {

char buf[20]= \; char buf2[] = \;

欢迎阅读

欢迎阅读

}

char *p1 = \;

char *p2 = malloc(100); strcpy(p2, \); system(\); return ;

3.5项目开发字符串模型 strstr-whiledowhile模型 两头堵模型 字符串反转模型

3.6一级指针(char *)易错模型分析

01char *(字符串)做函数参数出错模型分析 建立一个思想:是主调函数分配内存,还是被调用函数分配内存; //不要相信,主调函数给你传的内存空间,你可以写。。。。。。一级指针你懂了。 但是二级指针,你就不一定懂。。。抛出。。。。。。。。。 void copy_str21(char *from, char *to) { if (*NULL = '\\0' || *to!=’\\0’) { Printf(“func copy_str21() err\\n”); return; } for (; *from!='\\0'; from++, to++) { *to = *from; } *to = '\\0'; } //字符串逆序 int main() { //char p[1024] ={0}; char *p ={0}; p = NULL; char to[100]; copy_str21(p, to); C语言中没有你不知道的,只有你不会调 Java语言中没有你不会调的,只有你不知道 不断修改内存指针变量 02越界 越界 语法级别的越界 char buf[3] = \; 03不断修改指针变量的值 越界 欢迎阅读

欢迎阅读 void copy_str_err(char *from, char *to) { for (; *from!='\\0'; from++, to++) { *to = *from; } *to = '\\0'; printf(\, to); printf(\, from); } 04你向外面传递什么 1、临时str3内存空间 // char *str_cnct(x,y) /*简化算法*/ // char *x,*y; char *str_cnct(char *x, char* y) /*简化算法*/ { char str3[80]; char *z=str3; /*指针z指向数组str3*/ while(*z++=*x++); z--; /*去掉串尾结束标志*/ while(*z++=*y++); z=str3; /*将str3地址赋给指针变量z*/ return(z); } 2、经验要学习 while(*z++=*x++); z--; /*去掉串尾结束标志*/ char *str_cnct(char *x, char* y) /*简化算法*/ { char * str3= (char *)malloc(80) char *z=str3; /*指针z指向数组str3*/ while(*z++=*x++); z--; /*去掉串尾结束标志*/ while(*z++=*y++); z=str3; /*将str3地址赋给指针变量z*/ return(z); } char *str_cnct(char *x, char* y) /*简化算法*/ { If (x == NULL) { Return NULL; } char * str3= (char *)malloc(80) 欢迎阅读