A 'char * string' assigning problem

Discussion in 'C Programming' started by lili, Jul 17, 2009.

  1. lili

    lili Guest

    Please forgive my poor english.The following is my code:
    -------*************************************************---------
    #include <stdio.h>
    #include <string.h>

    int main()
    {
    char* teststr = "hello";
    char* yes = teststr;
    *(++yes) = 'w';
    printf(yes);
    }
    -------*************************************************---------
    print: segment error
    Why did i got a 'segment error'?
    but
    -------*************************************************---------
    #include <stdio.h>
    #include <string.h>

    int main()
    {
    char teststr[20] = "hello";
    char* yes = teststr;
    *(++yes) = 'w';
    printf(yes);
    }
    -------*************************************************---------
    print: hwllo
    or
    -------*************************************************---------
    #include <stdio.h>
    #include <string.h>

    int main()
    {
    char* teststr = "hello";
    char* yes = teststr;
    printf("%c\n", *(++yes));
    }
    -------*************************************************---------
    print: e

    is no problem.
    Can anyone help me , Thank you!
     
    lili, Jul 17, 2009
    #1
    1. Advertising

  2. lili <> writes:

    > Please forgive my poor english.The following is my code:
    > -------*************************************************---------
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main()
    > {
    > char* teststr = "hello";


    The arrays that result from a string literal are unmodifiable. The
    effect of doing so is undefined.

    > char* yes = teststr;
    > *(++yes) = 'w';


    'yes' is the same pointer as 'teststr' and 'teststr' points at an
    unmodifiable array of characters. Trying to put that 'w' where the
    'e' is in the original string causes anything to happen. A segfault
    one of the most helpful outcomes.

    Most people prefer to write:

    const char *teststr = "hello";

    so as to be reminded when an attempt is made to modify the target
    string.

    > printf(yes);
    > }
    > -------*************************************************---------
    > print: segment error
    > Why did i got a 'segment error'?
    > but
    > -------*************************************************---------
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main()
    > {
    > char teststr[20] = "hello";


    teststr is now a plain local array whose elements get initialised
    using the string hello. The chars in teststr can be modified with no
    ill effects.

    > char* yes = teststr;
    > *(++yes) = 'w';
    > printf(yes);
    > }
    > -------*************************************************---------
    > print: hwllo
    > or
    > -------*************************************************---------
    > #include <stdio.h>
    > #include <string.h>
    >
    > int main()
    > {
    > char* teststr = "hello";
    > char* yes = teststr;
    > printf("%c\n", *(++yes));


    And here there is no modification to cause a problem.

    > }
    > -------*************************************************---------
    > print: e
    >
    > is no problem.
    > Can anyone help me , Thank you!


    --
    Ben.
     
    Ben Bacarisse, Jul 17, 2009
    #2
    1. Advertising

  3. lili

    lili Guest

    Thank you Ben.I see
     
    lili, Jul 17, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Senthilraja

    Assigning string constant to char* without malloc

    Senthilraja, Aug 25, 2003, in forum: C Programming
    Replies:
    3
    Views:
    611
    Ben Pfaff
    Aug 25, 2003
  2. Steve Pope
    Replies:
    10
    Views:
    3,169
    Steve Pope
    Aug 26, 2006
  3. Peithon

    assigning const char* to char*

    Peithon, Jun 1, 2007, in forum: C Programming
    Replies:
    6
    Views:
    3,539
    Keith Thompson
    Jun 1, 2007
  4. thomas
    Replies:
    8
    Views:
    1,378
    Victor Bazarov
    Aug 21, 2012
  5. weston
    Replies:
    1
    Views:
    281
    Richard Cornford
    Sep 22, 2006
Loading...

Share This Page