What wrong with this simple function?

Discussion in 'C++' started by howa, Oct 14, 2006.

  1. howa

    howa Guest

    void reverse_string(char *str) {

    if (str == NULL)
    return;

    char tmp;
    size_t len = strlen(str);
    size_t mid = (int) len / 2;

    for (size_t i = 0; i < mid; i++) {

    tmp = str;
    str = str[len-i-1];
    str[len-i-1] = tmp;
    }
    }

    str can be reversed....
    howa, Oct 14, 2006
    #1
    1. Advertising

  2. howa

    Howard Guest

    "howa" <> wrote in message
    news:...
    > void reverse_string(char *str) {
    >
    > if (str == NULL)
    > return;
    >
    > char tmp;
    > size_t len = strlen(str);
    > size_t mid = (int) len / 2;
    >
    > for (size_t i = 0; i < mid; i++) {
    >
    > tmp = str;
    > str = str[len-i-1];
    > str[len-i-1] = tmp;
    > }
    > }
    >
    > str can be reversed....
    >


    How about you tell US what's wrong with it? Maybe we can tell you WHY.
    Does it compile? Does it crash? Does it work improperly? In what way?

    It helps if you tell us what problem you're having...

    -Howard
    Howard, Oct 14, 2006
    #2
    1. Advertising

  3. howa

    Daniel T. Guest

    "howa" <> wrote:

    > void reverse_string(char *str) {
    >
    > if (str == NULL)
    > return;
    >
    > char tmp;
    > size_t len = strlen(str);
    > size_t mid = (int) len / 2;
    >
    > for (size_t i = 0; i < mid; i++) {
    >
    > tmp = str;
    > str = str[len-i-1];
    > str[len-i-1] = tmp;
    > }
    > }
    >
    > str can be reversed....


    Why do you think something is wrong with it? What output were you
    expecting, and what output did you get?

    --
    There are two things that simply cannot be doubted, logic and perception.
    Doubt those, and you no longerÊhave anyone to discuss your doubts with,
    nor any ability to discuss them.
    Daniel T., Oct 14, 2006
    #3
  4. howa

    howa Guest

    // full listing

    #include <iostream>

    using namespace std;

    void reverse_string(char *str) {

    if (str == NULL)
    return;


    char tmp;
    size_t len = strlen(str);
    size_t mid = (int) len / 2;


    for (size_t i = 0; i < mid; i++) {


    tmp = str;
    str = str[len-i-1];
    str[len-i-1] = tmp;
    }



    }

    int main() {

    char *str = "apple";

    reverse_string(str);

    cout<<str;

    }



    // the program return 'apple', not 'elppa'
    Daniel T. 寫é“:

    > "howa" <> wrote:
    >
    > > void reverse_string(char *str) {
    > >
    > > if (str == NULL)
    > > return;
    > >
    > > char tmp;
    > > size_t len = strlen(str);
    > > size_t mid = (int) len / 2;
    > >
    > > for (size_t i = 0; i < mid; i++) {
    > >
    > > tmp = str;
    > > str = str[len-i-1];
    > > str[len-i-1] = tmp;
    > > }
    > > }
    > >
    > > str can be reversed....

    >
    > Why do you think something is wrong with it? What output were you
    > expecting, and what output did you get?
    >
    > --
    > There are two things that simply cannot be doubted, logic and perception.
    > Doubt those, and you no longerÊhave anyone to discuss your doubts with,
    > nor any ability to discuss them.
    howa, Oct 15, 2006
    #4
  5. howa

    xhy_China Guest

    you can replace the following sentence
    char *str = "apple";
    by char str[]=""apple;
    xhy_China, Oct 15, 2006
    #5
  6. howa

    howa Guest

    xhy_China 寫é“:

    > you can replace the following sentence
    > char *str = "apple";
    > by char str[]=""apple;


    that great! thanks....

    btw, what is the difference ?

    char *str = "apple";
    char str[] = "apple";

    both `str` are pointer to the beginning to the string, isn't?
    howa, Oct 15, 2006
    #6
  7. howa

    Jim Langston Guest

    "howa" <> wrote in message
    news:...

    xhy_China ??:

    >> you can replace the following sentence
    >> char *str = "apple";
    >> by char str[]=""apple;

    >
    >that great! thanks....
    >
    >.btw, what is the difference ?
    >
    >.char *str = "apple";


    Here str is a char pointer pointing to a *constant* string

    >char str[] = "apple";


    Here str is a char array initialized to the string "apple"

    >both `str` are pointer to the beginning to the string, isn't?


    Notice, one is constant, one isn't.
    Jim Langston, Oct 15, 2006
    #7
  8. howa

    Kai-Uwe Bux Guest

    howa wrote:

    >
    > xhy_China ???
    >
    >> you can replace the following sentence
    >> char *str = "apple";
    >> by char str[]=""apple;

    >
    > that great! thanks....
    >
    > btw, what is the difference ?
    >
    > char *str = "apple";
    > char str[] = "apple";
    >
    > both `str` are pointer to the beginning to the string, isn't?


    Nope: the first is a char* that points to the first character of a string
    literal (which is constant and that causes the UB you experienced). The
    second is an array of char that has been initialized to contain

    { 'a', 'p', 'p', 'l', 'e', 0 }

    This array is not declared const and you can modify the contents (although
    you cannot change its address or length). The two critters are of utterly
    different type and assignments only work one way:

    #include <iostream>

    int main ( void ) {
    char * c_ptr = 0;
    char c_arr [] = "apple";
    c_arr = c_ptr; // fails to compile!
    c_ptr = c_arr; // works: c_ptr now points to c_arr[0].
    std::cout << c_ptr << '\n';
    }



    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 15, 2006
    #8
  9. howa

    howa Guest

    Kai-Uwe Bux 寫é“:

    > howa wrote:
    >
    > >
    > > xhy_China ???
    > >
    > >> you can replace the following sentence
    > >> char *str = "apple";
    > >> by char str[]=""apple;

    > >
    > > that great! thanks....
    > >
    > > btw, what is the difference ?
    > >
    > > char *str = "apple";
    > > char str[] = "apple";
    > >
    > > both `str` are pointer to the beginning to the string, isn't?

    >
    > Nope: the first is a char* that points to the first character of a string
    > literal (which is constant and that causes the UB you experienced). The
    > second is an array of char that has been initialized to contain
    >
    > { 'a', 'p', 'p', 'l', 'e', 0 }
    >
    > This array is not declared const and you can modify the contents (although
    > you cannot change its address or length). The two critters are of utterly
    > different type and assignments only work one way:
    >
    > #include <iostream>
    >
    > int main ( void ) {
    > char * c_ptr = 0;
    > char c_arr [] = "apple";
    > c_arr = c_ptr; // fails to compile!
    > c_ptr = c_arr; // works: c_ptr now points to c_arr[0].
    > std::cout << c_ptr << '\n';
    > }
    >
    >
    >
    > Best
    >
    > Kai-Uwe Bux


    so is that mean

    1. if i do not want to modify the value

    char *str = "apple" or char str[] = "apple"

    the usage dose not matter, the pointer can be used in the same way

    2. if i want to modify the value, i need to use char[]

    thanks.
    howa, Oct 15, 2006
    #9
  10. howa

    Kai-Uwe Bux Guest

    howa wrote:

    >
    > Kai-Uwe Bux ???
    >
    >> howa wrote:
    >>
    >> >
    >> > xhy_China ???
    >> >
    >> >> you can replace the following sentence
    >> >> char *str = "apple";
    >> >> by char str[]=""apple;
    >> >
    >> > that great! thanks....
    >> >
    >> > btw, what is the difference ?
    >> >
    >> > char *str = "apple";
    >> > char str[] = "apple";
    >> >
    >> > both `str` are pointer to the beginning to the string, isn't?

    >>
    >> Nope: the first is a char* that points to the first character of a string
    >> literal (which is constant and that causes the UB you experienced). The
    >> second is an array of char that has been initialized to contain
    >>
    >> { 'a', 'p', 'p', 'l', 'e', 0 }
    >>
    >> This array is not declared const and you can modify the contents
    >> (although you cannot change its address or length). The two critters are
    >> of utterly different type and assignments only work one way:
    >>
    >> #include <iostream>
    >>
    >> int main ( void ) {
    >> char * c_ptr = 0;
    >> char c_arr [] = "apple";
    >> c_arr = c_ptr; // fails to compile!
    >> c_ptr = c_arr; // works: c_ptr now points to c_arr[0].
    >> std::cout << c_ptr << '\n';
    >> }
    >>
    >>
    >>
    >> Best
    >>
    >> Kai-Uwe Bux

    >
    > so is that mean
    >
    > 1. if i do not want to modify the value
    >
    > char *str = "apple" or char str[] = "apple"
    >
    > the usage dose not matter, the pointer can be used in the same way


    If you do not want to modify, you should use const:

    char const * c_ptr = "apple";
    char const c_arr [] = "apple";


    > 2. if i want to modify the value, i need to use char[]


    If you want to modify, you should use std::string.


    Actually, you should use std::string anyway:

    std::string const banner = "apple";
    std::string buffer = "apple";

    Is there a reason that you want to make your life harder and your code less
    efficient by using char* or char[]?


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 15, 2006
    #10
  11. howa

    howa Guest

    Kai-Uwe Bux 寫é“:

    > howa wrote:
    >
    > >
    > > Kai-Uwe Bux ???
    > >
    > >> howa wrote:
    > >>
    > >> >
    > >> > xhy_China ???
    > >> >
    > >> >> you can replace the following sentence
    > >> >> char *str = "apple";
    > >> >> by char str[]=""apple;
    > >> >
    > >> > that great! thanks....
    > >> >
    > >> > btw, what is the difference ?
    > >> >
    > >> > char *str = "apple";
    > >> > char str[] = "apple";
    > >> >
    > >> > both `str` are pointer to the beginning to the string, isn't?
    > >>
    > >> Nope: the first is a char* that points to the first character of a string
    > >> literal (which is constant and that causes the UB you experienced). The
    > >> second is an array of char that has been initialized to contain
    > >>
    > >> { 'a', 'p', 'p', 'l', 'e', 0 }
    > >>
    > >> This array is not declared const and you can modify the contents
    > >> (although you cannot change its address or length). The two critters are
    > >> of utterly different type and assignments only work one way:
    > >>
    > >> #include <iostream>
    > >>
    > >> int main ( void ) {
    > >> char * c_ptr = 0;
    > >> char c_arr [] = "apple";
    > >> c_arr = c_ptr; // fails to compile!
    > >> c_ptr = c_arr; // works: c_ptr now points to c_arr[0].
    > >> std::cout << c_ptr << '\n';
    > >> }
    > >>
    > >>
    > >>
    > >> Best
    > >>
    > >> Kai-Uwe Bux

    > >
    > > so is that mean
    > >
    > > 1. if i do not want to modify the value
    > >
    > > char *str = "apple" or char str[] = "apple"
    > >
    > > the usage dose not matter, the pointer can be used in the same way

    >
    > If you do not want to modify, you should use const:
    >
    > char const * c_ptr = "apple";
    > char const c_arr [] = "apple";
    >
    >
    > > 2. if i want to modify the value, i need to use char[]

    >
    > If you want to modify, you should use std::string.
    >
    >
    > Actually, you should use std::string anyway:
    >
    > std::string const banner = "apple";
    > std::string buffer = "apple";
    >
    > Is there a reason that you want to make your life harder and your code less
    > efficient by using char* or char[]?
    >
    >
    > Best
    >
    > Kai-Uwe Bux


    so can i say that

    char *str = "apple"; is the same as char const *str = "apple";

    as the value can't be modified anyway?
    howa, Oct 15, 2006
    #11
  12. howa

    Kai-Uwe Bux Guest

    howa wrote:
    [snip]
    > so can i say that
    >
    > char *str = "apple"; is the same as char const *str = "apple";
    >
    > as the value can't be modified anyway?


    The difference is that with

    char const * str = "apple";

    the compiler will barf at an attempt to modifying str. With

    char * str = "apple";

    an attempt to modify str might go undetected but it will be undefined
    behavior.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Oct 15, 2006
    #12
  13. howa

    Old Wolf Guest

    howa wrote:
    > void reverse_string(char *str) {
    >
    > if (str == NULL)
    > return;
    >
    > char tmp;
    > size_t len = strlen(str);
    > size_t mid = (int) len / 2;


    Worse than useless cast. Without the cast, your code works for
    all strings up to SIZE_MAX in length. With the cast, your code
    only works for strings up to INT_MAX in length.
    Old Wolf, Oct 16, 2006
    #13
  14. howa

    howa Guest

    Kai-Uwe Bux 寫é“:

    > howa wrote:
    > [snip]
    > > so can i say that
    > >
    > > char *str = "apple"; is the same as char const *str = "apple";
    > >
    > > as the value can't be modified anyway?

    >
    > The difference is that with
    >
    > char const * str = "apple";
    >
    > the compiler will barf at an attempt to modifying str. With
    >
    > char * str = "apple";
    >
    > an attempt to modify str might go undetected but it will be undefined
    > behavior.
    >
    >
    > Best
    >
    > Kai-Uwe Bux


    so is that mean

    char * str = "apple";

    is useless,

    we should consider use char const * str = "apple";
    howa, Oct 16, 2006
    #14
  15. howa

    Marcus Kwok Guest

    howa <> wrote:
    > Kai-Uwe Bux ?????????
    >> The difference is that with
    >>
    >> char const * str = "apple";
    >>
    >> the compiler will barf at an attempt to modifying str. With
    >>
    >> char * str = "apple";
    >>
    >> an attempt to modify str might go undetected but it will be undefined
    >> behavior.

    >
    > so is that mean
    >
    > char * str = "apple";
    >
    > is useless,


    Maybe not useless, but dangerous and not recommended. Basically the
    only reason this is allowed is to be able to interface with legacy
    functions that are not const-correct. You should only be using this if
    you are 100% absolutely sure that the function you are passing it to
    does not attempt to modify the string.

    > we should consider use char const * str = "apple";


    This is better.

    --
    Marcus Kwok
    Replace 'invalid' with 'net' to reply
    Marcus Kwok, Oct 17, 2006
    #15
    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. Miguel Dias Moura
    Replies:
    1
    Views:
    402
    Lars Netzel
    Jun 18, 2004
  2. Peter van der Goes

    Simple Code? What's Wrong?

    Peter van der Goes, Jul 14, 2005, in forum: Java
    Replies:
    4
    Views:
    359
    Mike Schilling
    Jul 14, 2005
  3. Replies:
    9
    Views:
    22,785
  4. Matthew
    Replies:
    7
    Views:
    644
    Priscilla Walmsley
    Jan 7, 2005
  5. RichardOnRails
    Replies:
    3
    Views:
    117
    RichardOnRails
    Jul 21, 2008
Loading...

Share This Page