程序运行到
*tmp_str = *(tmp_str+length);
时总是失败。什么SIGSEGV错误,好像是内存的问题。但是运行
printf("%c",*tmp_str); printf("%c\n",*(tmp_str+length));
这两句却没问题。
后来发现这两个指针可以取值,但不能赋值。
在main里面的声明为
char *test_str; char *test_substr; test_str = "I am an excellent coder!"; test_substr = "an ";
想起书上说的指针加减只能在对象为数组时才行,但是可以读取为什么不能赋值呢?而且是在main里声明,再通过子函数参数传递的,
del_substr( test_str, test_substr );
把
length = tmp_str - tmp_pst;
for( tmp_str = tmp_pst; *( tmp_str + length ) != '\0'; tmp_str++){
*tmp_str = *(tmp_str+length);//
}
改成
length = tmp_str - tmp_pst; for( tmp_pst; tmp_str!= '\0'; tmp_str++){ *tmp_str = *tmp_pst; tmp_pst++; }
没有直接在*取值里做加减运算也不行。
试来试去没办法,声明改成
char test_str[128] = "I am an excellent coder!"; char test_substr[128]= "an ";
就行了。
我勒个去啊,就是数组的问题。

浙公网安备 33010602011771号