jw_lee

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::

程序运行到

*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 ";

就行了。

我勒个去啊,就是数组的问题。

posted on 2013-08-01 22:08  jw_lee  阅读(210)  评论(0)    收藏  举报