CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2007
    Posts
    149

    CStrings Question: What is the difference between char str[] and char* str

    Hi;

    I guess I'm a newbie with this question.

    My question is about C strings.


    I'm trying to figure out why the behavior of these 2 strings are different as it relates to toupper() function ?

    char str1[] ="For the perfecting of the saints";
    char* str2 ="For the perfecting of the saints";


    //when I call ucase(str1) everything works perfectly.

    ucase(str1);

    //but when i call

    ucase(str2)

    //it crashes. Can you tell me what's the difference between char str1[] and char* str2?



    Here's the ucase() function i'm calling below.

    //convert to uppercase
    void ucase(char* buffer)
    {
    int len = strlen(buffer);

    for(int i = 0; i < len; i++)
    {
    buffer[i] = toupper(buffer[i]);
    }
    }




    I can loop through both just fine using something like this
    for(int i =0; i<36; i++){
    printf("here %c\n", str1[i]);
    }

    but when it seems to me toupper breaks when you use it with char* x for some reason.

    Thanks in Advance.

    Stev

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: CStrings Question: What is the difference between char str[] and char* str

    char[] is an array of char.
    Code:
    char str1[] ="For the perfecting of the saints";
    str1 is an array of char initialized with a string literal.

    char* is a pointer to char
    Code:
    char* str2 ="For the perfecting of the saints";
    str2 is a pointer to char holding the address of the first char of the string literal (which is immutable, i.e. you cannot change; attempting to do that is yields undefined behaviour). Hence it should rather be:
    Code:
    const char* str2 ="For the perfecting of the saints";
    Last edited by cilu; April 26th, 2007 at 06:31 AM.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: CStrings Question: What is the difference between char str[] and char* str

    http://www.codeguru.com/forum/showth...string+literal
    http://www.codeguru.com/forum/showth...string+literal

    Everytime you try modifying a string literal, God kills a kitten! And your program as well...

    Cheers
    B+!
    'There is no cat' - A. Einstein

    Use &#91;code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

  4. #4
    Join Date
    Jun 2006
    Posts
    437

    Re: CStrings Question: What is the difference between char str[] and char* str

    Hi all.

    In addition what Cilu said, char[] defines an array of char initialized with a string literal and closed by the '\0', the special character that ends the strings;
    When you call ucase with char[], the function strlen finds the '\0' and all works fine; instead, when you call ucase with char* strlen don't find the '\0' so your program crashes.

  5. #5
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: CStrings Question: What is the difference between char str[] and char* str

    Quote Originally Posted by davide++
    When you call ucase with char[], the function strlen finds the '\0' and all works fine; instead, when you call ucase with char* strlen don't find the '\0' so your program crashes.
    strlen works fine with char* variables as given in the post.

    The problem is that ucase attempts to modify a string literal.
    This results in undefined behavior. It might do what you want,
    it might not. (For example, ucase would not crash with
    VC++ version 5, but will for versions after that.).

  6. #6
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: CStrings Question: What is the difference between char str[] and char* str

    Quote Originally Posted by cilu
    Hence it should rather be:
    Code:
    const char* str2 ="For the perfecting of the saints";
    no it should be
    Code:
    char const* str2 ="For the perfecting of the saints";
    just try compiling this:

    Code:
    const char* str1 ="For the perfecting of the saints";
    char const* str2 ="For the perfecting of the saints";
    str2 = "nooooooooooooot";
    str1 = "nooooooooooooot";

  7. #7
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: CStrings Question: What is the difference between char str[] and char* str

    Quote Originally Posted by Mitsukai
    no it should be

    Code:
    char const* str2 ="For the perfecting of the saints";
    What's the difference?

    - petter

  8. #8
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,125

    Re: CStrings Question: What is the difference between char str[] and char* str

    "const" only matters chich side of the "*" it is on. To the left (either before or after the type), the DATA is const. To the right of the "*", the pointer is const (must always point to the same location), but does not imply that the data is const.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  9. #9
    Join Date
    Jan 2006
    Location
    Belo Horizonte, Brazil
    Posts
    405

    Re: CStrings Question: What is the difference between char str[] and char* str

    Hi Mitsukai.

    Quote Originally Posted by Mitsukai
    no it should be
    Code:
    char const* str2 ="For the perfecting of the saints";
    Both char const* and const char* should mean the same thing. What compiler are you using? The important thing is that the asterisc is to the right of the const keyword.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured