Can't figure this out

Discussion in 'C Programming' started by Jeff, Nov 18, 2003.

  1. Jeff

    Jeff Guest

    Hello everybody,

    I just have a little problem, and I just can't figure out what it is:

    [gongds @ labtherm][15:52:28]$ cat test.c
    #include <stdio.h>
    int main (void)
    {
    char* s = "CAT";

    *(s+1) = 'B';
    printf("%s", s);

    return (0);
    }
    [gongds @ labtherm][15:52:32]$ gcc -o test test.c
    [gongds @ labtherm][15:52:40]$ ./test
    Segmentation fault (core dumped)
    [gongds @ labtherm][15:52:42]$

    Just what am I doing wrong here? Just for information this 'code' was
    compiled using GCC under cygwin, on a windows 98 machine.

    Thank-you for any help,
    Cheers!

    ---
    Joseph
     
    Jeff, Nov 18, 2003
    #1
    1. Advertising

  2. Jeff

    Allan Bruce Guest

    "Jeff" <> wrote in message
    news:...
    > Hello everybody,
    >
    > I just have a little problem, and I just can't figure out what it is:
    >
    > [gongds @ labtherm][15:52:28]$ cat test.c
    > #include <stdio.h>
    > int main (void)
    > {
    > char* s = "CAT";
    >
    > *(s+1) = 'B';
    > printf("%s", s);
    >
    > return (0);
    > }
    > [gongds @ labtherm][15:52:32]$ gcc -o test test.c
    > [gongds @ labtherm][15:52:40]$ ./test
    > Segmentation fault (core dumped)
    > [gongds @ labtherm][15:52:42]$
    >
    > Just what am I doing wrong here? Just for information this 'code' was
    > compiled using GCC under cygwin, on a windows 98 machine.
    >
    > Thank-you for any help,
    > Cheers!
    >


    I think the problem is that you are declaring your pointer to point to a
    string literal which you cannot write to, so when you try you are getting a
    core dump.
    Allan
     
    Allan Bruce, Nov 18, 2003
    #2
    1. Advertising

  3. "Allan Bruce" <> wrote in
    news:bpdcon$110$2surf.net:

    >> int main (void)
    >> {
    >> char* s = "CAT";
    >>
    >> *(s+1) = 'B';


    BANG! You may have just attempted to write to a string literal in ROM,
    FLASH, TLB protected RAM, etc.

    >> printf("%s", s);
    >>
    >> return (0);
    >> }
    >>
    >> Just what am I doing wrong here? Just for information this 'code' was
    >> compiled using GCC under cygwin, on a windows 98 machine.


    The compiler and platform should never matter for ISO C code.

    > I think the problem is that you are declaring your pointer to point to a
    > string literal which you cannot write to, so when you try you are
    > getting > a core dump.


    Correct. You cannot do what the OP is trying to do and expect it to work.
    To the OP: What if the compiler placed "CAT" into ROM or FLASH?

    --
    - Mark ->
    --
     
    Mark A. Odell, Nov 18, 2003
    #3
  4. (Jeff) wrote:

    > I just have a little problem, and I just can't figure out what it is:
    >
    > #include <stdio.h>
    > int main (void)
    > {
    > char* s = "CAT";
    >
    > *(s+1) = 'B';
    > printf("%s", s);
    >
    > return (0);
    > }
    > Segmentation fault (core dumped)
    >
    > Just what am I doing wrong here? Just for information this 'code' was
    > compiled using GCC under cygwin, on a windows 98 machine.


    You are not allowed to change string literals; they may reside in
    read-only memory. To make your code work, change the declaration/
    initialization of s to:

    char s[] = "CAT";

    s is now declared as an array of four(!) characters and initialized
    with 'C''A''T''\0' (as opposed to being declared as a character
    pointer pointing to a string literal in your original code).

    HTH
    Regards

    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Nov 18, 2003
    #4
  5. Jeff

    Eric Sosman Guest

    Jeff wrote:
    >
    > Hello everybody,
    >
    > I just have a little problem, and I just can't figure out what it is:
    >
    > [gongds @ labtherm][15:52:28]$ cat test.c
    > #include <stdio.h>
    > int main (void)
    > {
    > char* s = "CAT";
    >
    > *(s+1) = 'B';
    > printf("%s", s);
    >
    > return (0);
    > }
    > [gongds @ labtherm][15:52:32]$ gcc -o test test.c
    > [gongds @ labtherm][15:52:40]$ ./test
    > Segmentation fault (core dumped)
    > [gongds @ labtherm][15:52:42]$
    >
    > Just what am I doing wrong here? Just for information this 'code' was
    > compiled using GCC under cygwin, on a windows 98 machine.


    This is Question 16.6 in the comp.lang.c Frequently
    Asked Questions (FAQ) list

    http://www.eskimo.com/~scs/C-faq/faq.html

    --
     
    Eric Sosman, Nov 18, 2003
    #5
  6. Jeff

    Ian Woods Guest

    (Jeff) wrote in news:6c79eab5.0311180700.75725fb8
    @posting.google.com:

    > Hello everybody,
    >
    > I just have a little problem, and I just can't figure out what it is:
    >
    > [gongds @ labtherm][15:52:28]$ cat test.c
    > #include <stdio.h>
    > int main (void)
    > {
    > char* s = "CAT";


    s pointer to a string literal.

    > *(s+1) = 'B';


    You're trying to modify the string literal. That's undefined behaviour
    IIRC, hence the nasal demons.

    > printf("%s", s);
    >
    > return (0);
    > }
    >
    > Just what am I doing wrong here? Just for information this 'code' was
    > compiled using GCC under cygwin, on a windows 98 machine.


    See: http://www.eskimo.com/~scs/C-faq/q16.6.html

    Ian Woods
     
    Ian Woods, Nov 18, 2003
    #6
  7. Jeff

    Derk Gwen Guest

    (Jeff) wrote:
    # Hello everybody,
    #
    # I just have a little problem, and I just can't figure out what it is:
    #
    # [gongds @ labtherm][15:52:28]$ cat test.c
    # #include <stdio.h>
    # int main (void)
    # {
    # char* s = "CAT";
    #
    # *(s+1) = 'B';
    # printf("%s", s);
    #
    # return (0);
    # }
    # [gongds @ labtherm][15:52:32]$ gcc -o test test.c
    # [gongds @ labtherm][15:52:40]$ ./test
    # Segmentation fault (core dumped)
    # [gongds @ labtherm][15:52:42]$
    #
    # Just what am I doing wrong here? Just for information this 'code' was
    # compiled using GCC under cygwin, on a windows 98 machine.

    s points to a literal string. Some implementations permit literal strings
    to be modified; others out literal strings in an unwritable page, and
    attempts to write to them will get segmentation faults or other memory
    related errors. If you want to be able to modify characters of s without
    making the string longer, do

    char s[] = "CAT";

    Enough writable space will be allocated to s to hold the string, and
    the string will be copied into the space.

    (cd /tmp
    cat <<':eof' >t.c
    #include <stdio.h>
    int main (int N,char **P)
    {
    char s[] = "CAT";

    *(s+1) = 'B';
    printf("%s", s);

    return (0);
    }
    :eof
    cc t.c; /tmp/a.out)

    CBT

    --
    Derk Gwen http://derkgwen.250free.com/html/index.html
    I have no idea what you just said.
    I get that alot.
     
    Derk Gwen, Nov 18, 2003
    #7
  8. Jeff

    Dan Pop Guest

    In <Xns9437682C6F757CopyrightMarkOdell@130.133.1.4> "Mark A. Odell" <> writes:

    >news:bpdcon$110$2surf.net:
    >
    >>> Just what am I doing wrong here? Just for information this 'code' was
    >>> compiled using GCC under cygwin, on a windows 98 machine.

    >
    >The compiler and platform should never matter for ISO C code.


    For ISO C code that works. If it doesn't, specifying this kind of
    information can be helpful in identifying the origin of the problem.

    >Correct. You cannot do what the OP is trying to do and expect it to work.
    >To the OP: What if the compiler placed "CAT" into ROM or FLASH?


    OTOH, since we know the OP's compiler, we can also recommend
    -fwritable-strings for code that, for one reason or another, *must* be
    able to write into string literals.

    This is, of course, to be avoided in new code, but there is still old
    code floating around that relies on writable string literals, because
    this is how they were before ANSI C. Most commercial compilers simply
    don't take advantage of the optimisations allowed by the standard in this
    area, in order to avoid breaking such code. gcc's solution was to
    provide -fwritable-strings and use read-only string literals by default,
    which is a good thing, because it prevents beginners from writing
    broken code that works by accident (the typical beginner doesn't
    use -fwritable-strings).

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Nov 18, 2003
    #8
  9. On 18 Nov 2003 07:00:29 -0800, in comp.lang.c ,
    (Jeff) wrote:

    >Hello everybody,
    >
    >I just have a little problem, and I just can't figure out what it is:
    >
    > char* s = "CAT";


    s points to a string literal. These are nonmodifiable.

    > *(s+1) = 'B';


    you tried to modify it - *bang*

    This is a FAQ by the way - its well worth reading that doc before
    posting here.


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.angelfire.com/ms3/bchambless0/welcome_to_clc.html>
     
    Mark McIntyre, Nov 18, 2003
    #9
  10. Jeff

    Jeff Guest

    Thank-you very much for all your help.

    I am sorry for not having seen this in the FAQ. I had actually checked
    the FAQ, but only in the pointer section, thus missing the crucial
    question ;)

    Thanks again.

    ---
    Joseph
     
    Jeff, Nov 19, 2003
    #10
  11. Jeff

    Mike Wahler Guest

    "Jeff" <> wrote in message
    news:...
    > Thank-you very much for all your help.
    >
    > I am sorry for not having seen this in the FAQ. I had actually checked
    > the FAQ, but only in the pointer section, thus missing the crucial
    > question ;)


    Evidence for the oft given advice here:
    Read The Entire FAQ. And even after that,
    it's still typically helpful to review it
    periodically. All of it. :)

    -Mike
     
    Mike Wahler, Nov 19, 2003
    #11
    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. logia
    Replies:
    6
    Views:
    651
  2. Chad A. Beckner

    Please Help! Can't figure this out...

    Chad A. Beckner, Jun 18, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    401
    bloomfield
    Jun 21, 2004
  3. myleslawrence

    Simple but I can't figure it out

    myleslawrence, Jan 19, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    321
    IPGrunt
    Jan 19, 2005
  4. Wayne  Wengert

    Can't Figure Out What is Wrong

    Wayne Wengert, Feb 27, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    450
    Michael H
    Feb 28, 2005
  5. Albert Hopkins
    Replies:
    3
    Views:
    470
    Steven D'Aprano
    Dec 10, 2008
Loading...

Share This Page