why does this not work?

Discussion in 'C Programming' started by Matt Kowalczyk, May 21, 2006.

  1. How come this function doesn't work?

    void shift(char** list, int index) {
    char buff = (*list)[0];
    memmove((*list)+1, *list, index);
    (*list)[0] = buff;
    return;
    }

    I call it like so:

    char* t = "abcde";

    shift(&t, 1);

    I expect t = "bacde"

    or

    shift(&t, 0) would not effect t at all.

    Anyway, I made it work by creating another character array inside the function.
    I then did a memcpy to copy list to this temp buffer and the memmove function
    worked correctly when I passed in the temp buffer local to the function. I
    would like to understand why the above code will not work. Am I doing something
    wrong?

    Thanks,
    Matt
     
    Matt Kowalczyk, May 21, 2006
    #1
    1. Advertising

  2. >How come this function doesn't work?

    If you attempt to write on a quoted string literal, you invoke
    the wrath of undefined behavior.

    What *does* it do on your machine? Smegmentation fault?

    >void shift(char** list, int index) {
    > char buff = (*list)[0];
    > memmove((*list)+1, *list, index);
    > (*list)[0] = buff;
    > return;
    >}
    >
    >I call it like so:
    >
    >char* t = "abcde";
    >
    >shift(&t, 1);
    >
    >I expect t = "bacde"
    >
    >or
    >
    >shift(&t, 0) would not effect t at all.
    >
    >Anyway, I made it work by creating another character array inside the function.
    >I then did a memcpy to copy list to this temp buffer and the memmove function
    >worked correctly when I passed in the temp buffer local to the function. I
    >would like to understand why the above code will not work. Am I doing something
    >wrong?


    You are attempting to write on a string literal. That's bad.

    Gordon L. Burditt
     
    Gordon Burditt, May 21, 2006
    #2
    1. Advertising

  3. Matt Kowalczyk

    pete Guest

    Gordon Burditt wrote:
    >
    > >How come this function doesn't work?

    >
    > If you attempt to write on a quoted string literal, you invoke
    > the wrath of undefined behavior.
    >
    > What *does* it do on your machine? Smegmentation fault?
    >
    > >void shift(char** list, int index) {
    > > char buff = (*list)[0];
    > > memmove((*list)+1, *list, index);
    > > (*list)[0] = buff;
    > > return;
    > >}
    > >
    > >I call it like so:
    > >
    > >char* t = "abcde";
    > >
    > >shift(&t, 1);
    > >
    > >I expect t = "bacde"
    > >
    > >or
    > >
    > >shift(&t, 0) would not effect t at all.
    > >
    > >Anyway,
    > >I made it work by creating another character array
    > >inside the function.
    > >I then did a memcpy to copy list to this temp buffer
    > >and the memmove function
    > >worked correctly when I passed in the temp buffer
    > >local to the function.
    > >I would like to understand why the above code will not work.
    > >Am I doing something
    > >wrong?

    >
    > You are attempting to write on a string literal. That's bad.


    Also,
    the function is overly complicated
    concerning levels of indirection,
    and bear in mind that it is undefined
    if strlen(t) is less than 2.

    /* BEGIN new.c */

    #include <stdio.h>
    #include <string.h>

    void shift(char* list, size_t index);

    int main (void)
    {
    char t[] = "abcde";

    shift(t, 1);
    puts(t);
    return 0;
    }

    void shift(char* list, size_t index)
    {
    char buf = list[1];

    memmove(list + 1, list, index);
    list[0] = buf;
    }

    /* END new.c */

    --
    pete
     
    pete, May 21, 2006
    #3
  4. Gordon Burditt wrote:
    >>How come this function doesn't work?

    >
    >
    > If you attempt to write on a quoted string literal, you invoke
    > the wrath of undefined behavior.
    >
    > What *does* it do on your machine? Smegmentation fault?
    >
    >
    >>void shift(char** list, int index) {
    >> char buff = (*list)[0];
    >> memmove((*list)+1, *list, index);
    >> (*list)[0] = buff;
    >> return;
    >>}
    >>
    >>I call it like so:
    >>
    >>char* t = "abcde";
    >>
    >>shift(&t, 1);
    >>
    >>I expect t = "bacde"
    >>
    >>or
    >>
    >>shift(&t, 0) would not effect t at all.
    >>
    >>Anyway, I made it work by creating another character array inside the function.
    >>I then did a memcpy to copy list to this temp buffer and the memmove function
    >>worked correctly when I passed in the temp buffer local to the function. I
    >>would like to understand why the above code will not work. Am I doing something
    >>wrong?

    >
    >
    > You are attempting to write on a string literal. That's bad.
    >
    > Gordon L. Burditt


    It generated a segmentation fault. I thought that I would need to pass in a
    poniter to the string that I was modifying for the changes to effect to the
    string outside the function.

    I'm going to try the code that pete posted to see if it will work. I still
    don't understand why my function wasn't working. Could you explain writing on a
    string literal?

    Assuming pete's code works with this function signature:

    void shift(char* list, size_t index)

    why would it fail with my signature? I'm just passing a pointer to the string.

    void shift(char** list, size_t index)?

    /* BEGIN pete's code */

    #include <stdio.h>
    #include <string.h>

    void shift(char* list, size_t index);

    int main (void)
    {
    char t[] = "abcde";

    shift(t, 1);
    puts(t);
    return 0;
    }

    void shift(char* list, size_t index)
    {
    char buf = list[1];

    memmove(list + 1, list, index);
    list[0] = buf;
    }

    /* END pete's code /*
     
    Matt Kowalczyk, May 21, 2006
    #4
  5. Matt Kowalczyk

    Default User Guest

    Gordon Burditt wrote:

    > > How come this function doesn't work?

    >
    > If you attempt to write on a quoted string literal, you invoke
    > the wrath of undefined behavior.



    I'm sorry, I don't know who you are talking to because don't have
    proper attributions. Please try to be responsible participant in the
    newsgroup.




    Brian
     
    Default User, May 21, 2006
    #5
  6. On Sun, 21 May 2006 00:07:40 -0700, Matt Kowalczyk wrote:

    > Could you explain writing on a
    > string literal?


    It is simply part of the specification of the language that you may not
    modify a string literal.

    char *s = "abcde";
    s[1] = 'x'; /* not allowed: t points to a string literal */

    char t[] = "abcde"; /* the array t is initialised with "abcde" */
    t[1] = 'x'; /* OK because t is an ordinary array */

    --
    Ben.
     
    Ben Bacarisse, May 21, 2006
    #6
    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. jblazi
    Replies:
    5
    Views:
    460
    jblazi
    Aug 16, 2004
  2. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    507
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    996
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,267
    Smokey Grindel
    Dec 2, 2006
  5. Tarun
    Replies:
    5
    Views:
    421
    Tarun
    Jul 14, 2005
Loading...

Share This Page