指针数组和数组的区别
这是一个删除子串的函数,如果把char str1[]= "123456789";改为char *str1="123456789";对函数体怎么改才能实现同样的功能程序代码:
#include <stdlib.h> #include <stdio.h> #define TRUE 1 #define FALSE 0 char *del_substr(char *str, char const *substr); int main() { char str1[]= "123456789"; char const * str2= "345"; printf("before delet: %s\n", str1); printf("will delete the chars: %s\n", str2); del_substr(str1, str2); printf("After delete: %s\n", str1); system("pause"); return 0; } char *del_substr(char *str, char const *substr) { char const * subP; char *strP; char *temp; int flag = TRUE; strP = str; if( (!*str) || *substr=='\0' ) //判断两个字符串是否有空字符串 return FALSE; //(!*str)和*str=='\0'是等价的 while(*strP) { temp = strP; subP = substr; for(; *subP; subP++) { if(*strP == *subP) { strP++; flag = FALSE; continue; } break; } if(*subP == '\0') { while(*temp++ = *strP++) ; break; } if(flag) strP++; } return TRUE; }