char *

Discussion in 'C++' started by Carmen Sei, Apr 5, 2008.

  1. Carmen Sei

    Carmen Sei Guest

    why the following compile is OK

    but will crash when execute?

    the problem seem to in -
    convertToUppercase( phrase );

    ==========
    // Converting lowercase letters to uppercase letters
    // using a non-constant pointer to non-constant data.
    #include <iostream>
    using std::cout;
    using std::endl;

    #include <cctype> // prototypes for islower and toupper
    using std::islower;
    using std::toupper;

    void convertToUppercase( char * );

    int main()
    {
    //char phrase[] = "characters and $32.98";

    char * phrase = "characters and $32.98";

    convertToUppercase( phrase );

    return 0; // indicates successful termination
    } // end main

    // convert string to uppercase letters
    void convertToUppercase( char *sPtr )
    {
    while ( *sPtr != '\0' ) // loop while current character is not '\0'
    {
    if ( islower( *sPtr ) ) // if character is lowercase,
    *sPtr = toupper( *sPtr ); // convert to uppercase

    sPtr++; // move sPtr to next character in string
    } // end while
    } // end function convertToUppercase
    Carmen Sei, Apr 5, 2008
    #1
    1. Advertising

  2. Carmen Sei

    Carmen Sei Guest

    to declare a string like

    "abcd"

    I should never use char * right?

    i should always use const char * for string declare?

    or i should always use char[]?
    Carmen Sei, Apr 5, 2008
    #2
    1. Advertising

  3. Carmen Sei

    Sharad Guest

    "Carmen Sei" <> wrote in message
    > why the following compile is OK
    >
    > but will crash when execute?
    >
    > the problem seem to in -
    > convertToUppercase( phrase );
    >
    > ==========
    > // Converting lowercase letters to uppercase letters
    > // using a non-constant pointer to non-constant data.
    > #include <iostream>
    > using std::cout;
    > using std::endl;
    >
    > #include <cctype> // prototypes for islower and toupper
    > using std::islower;
    > using std::toupper;
    >
    > void convertToUppercase( char * );
    >
    > int main()
    > {
    > //char phrase[] = "characters and $32.98";
    >
    > char * phrase = "characters and $32.98";
    >
    > convertToUppercase( phrase );
    >
    > return 0; // indicates successful termination
    > } // end main
    >
    > // convert string to uppercase letters
    > void convertToUppercase( char *sPtr )
    > {
    > while ( *sPtr != '\0' ) // loop while current character is not '\0'
    > {
    > if ( islower( *sPtr ) ) // if character is lowercase,
    > *sPtr = toupper( *sPtr ); // convert to uppercase
    >
    > sPtr++; // move sPtr to next character in string
    > } // end while
    > } // end function convertToUppercase


    Check a recent thread with subject "strcat" on this NG. In short, what are
    you trying to modify and is it modifiable?

    Sharad
    P.S. - Also, does it work when you make phrase an array (the commented code)
    and why?
    Sharad, Apr 5, 2008
    #3
  4. Carmen Sei

    Sharad Guest

    "Carmen Sei" <> wrote in message

    > to declare a string like
    >
    > "abcd"


    > I should never use char * right?


    No, depends on your intention. "abcd" is a *string literal*.

    > i should always use const char * for string declare?
    >
    > or i should always use char[]?


    You can't modify string literals in legal C++ programs. You can use an array
    if you have to modify the string, or even better look at std::string class.

    Sharad
    Sharad, Apr 5, 2008
    #4
  5. Carmen Sei

    Carmen Sei Guest

    are you meaing any string like

    "abcd" is imply a const char* automatically?

    which means if i assign "abcd" to a variable -
    char* var = "abcd";

    then I suppose not to modify the content of char* var???


    > what are
    >you trying to modify and is it modifiable?
    >
    Carmen Sei, Apr 5, 2008
    #5
  6. Carmen Sei

    Ian Collins Guest

    Carmen Sei wrote:

    [Please don't top-post]

    >
    >> what are
    >> you trying to modify and is it modifiable?
    >>

    > are you meaing any string like
    >
    > "abcd" is imply a const char* automatically?
    >
    > which means if i assign "abcd" to a variable -
    > char* var = "abcd";
    >

    One would expect a decent compiler to emit a warning here, not required,
    but useful.

    > then I suppose not to modify the content of char* var???
    >

    Yes, although the pointer should be const.

    --
    Ian Collins.
    Ian Collins, Apr 5, 2008
    #6
  7. Carmen Sei

    Jim Langston Guest

    Carmen Sei wrote:
    > are you meaing any string like
    >
    > "abcd" is imply a const char* automatically?
    >
    > which means if i assign "abcd" to a variable -
    > char* var = "abcd";
    >
    > then I suppose not to modify the content of char* var???


    "abcd" is a constant character array. What happens with it is a differnet
    story.

    char Foo[] = "abcd";
    Foo is allocated 5 bytes and the array "abcd" is copied into it.

    char Foo* = "abcd";
    Foo is allocated the size of a pointer and it points to the start of the
    constant char array. Now, it is up to you to remember if Foo is pointing to
    something you can change or not.

    char Foo[] = "abcd";
    char Bar* = "abcd";
    // Bar[0] = 'x'; // Illegal/undefined
    Bar = Foo;
    Bar[0] = 'x'; // Now legal/well defined

    The only reason that a char* can be assigned to point to a const char array
    such as "abcd" is for C compatability. It was legal in C and for C code to
    compile in C++ it is allowed there also. But shouldn't be done. Better is:

    const char* Foo = "abcd";

    Which if C++ didn't try to remain compatable with C would be the only legal
    way to do it.


    >
    >
    >> what are
    >> you trying to modify and is it modifiable?




    --
    Jim Langston
    Jim Langston, Apr 5, 2008
    #7
  8. Jim Langston wrote:

    > Carmen Sei wrote:
    >> are you meaing any string like
    >>
    >> "abcd" is imply a const char* automatically?
    >>
    >> which means if i assign "abcd" to a variable -
    >> char* var = "abcd";
    >>
    >> then I suppose not to modify the content of char* var???

    >
    > "abcd" is a constant character array. What happens with it is a differnet
    > story.
    >
    > char Foo[] = "abcd";
    > Foo is allocated 5 bytes and the array "abcd" is copied into it.
    >
    > char Foo* = "abcd";
    > Foo is allocated the size of a pointer and it points to the start of the
    > constant char array. Now, it is up to you to remember if Foo is pointing
    > to something you can change or not.
    >
    > char Foo[] = "abcd";
    > char Bar* = "abcd";
    > // Bar[0] = 'x'; // Illegal/undefined
    > Bar = Foo;
    > Bar[0] = 'x'; // Now legal/well defined
    >
    > The only reason that a char* can be assigned to point to a const char
    > array
    > such as "abcd" is for C compatability. It was legal in C and for C code
    > to
    > compile in C++ it is allowed there also. But shouldn't be done.


    This is such an inconsistent and disgusting language "feature" that it
    should be removed, IMHO. Are there any plans to do so?

    Sometimes you have to break backwards compatibility (as does for example
    every new keyword). People with legacy code bases probably can rely on
    compiler switches for non standard behaviour or, better yet, simply should
    fix their code. (I realise this could me very tedious for large code
    bases.)
    Paul Brettschneider, Apr 5, 2008
    #8
  9. Carmen Sei

    Jim Langston Guest

    Paul Brettschneider wrote:
    > Jim Langston wrote:
    >
    >> Carmen Sei wrote:
    >>> are you meaing any string like
    >>>
    >>> "abcd" is imply a const char* automatically?
    >>>
    >>> which means if i assign "abcd" to a variable -
    >>> char* var = "abcd";
    >>>
    >>> then I suppose not to modify the content of char* var???

    >>
    >> "abcd" is a constant character array. What happens with it is a
    >> differnet story.
    >>
    >> char Foo[] = "abcd";
    >> Foo is allocated 5 bytes and the array "abcd" is copied into it.
    >>
    >> char Foo* = "abcd";
    >> Foo is allocated the size of a pointer and it points to the start of
    >> the constant char array. Now, it is up to you to remember if Foo is
    >> pointing to something you can change or not.
    >>
    >> char Foo[] = "abcd";
    >> char Bar* = "abcd";
    >> // Bar[0] = 'x'; // Illegal/undefined
    >> Bar = Foo;
    >> Bar[0] = 'x'; // Now legal/well defined
    >>
    >> The only reason that a char* can be assigned to point to a const char
    >> array
    >> such as "abcd" is for C compatability. It was legal in C and for C
    >> code to
    >> compile in C++ it is allowed there also. But shouldn't be done.

    >
    > This is such an inconsistent and disgusting language "feature" that it
    > should be removed, IMHO. Are there any plans to do so?
    >
    > Sometimes you have to break backwards compatibility (as does for
    > example every new keyword). People with legacy code bases probably
    > can rely on compiler switches for non standard behaviour or, better
    > yet, simply should fix their code. (I realise this could me very
    > tedious for large code bases.)


    I personally think it would be a good idea to not allow it be default and
    allow some compiler switch to turn on the "feature". Or at least have the
    compiler issue a warning about it. I doubt the "feature" will go away in
    the standard any time soon though.

    --
    Jim Langston
    Jim Langston, Apr 6, 2008
    #9
    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. wwj
    Replies:
    7
    Views:
    525
  2. wwj
    Replies:
    24
    Views:
    2,462
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    454
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    563
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    991
    Ian Collins
    May 9, 2006
Loading...

Share This Page