String and Pointer to a String

This is a discussion on String and Pointer to a String within the C++ Programming forums, part of the General Programming Boards category; Hi, as I compile the following code, I dont get an error during compilation, but during execution. If I leave ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Germany
    Posts
    12

    String and Pointer to a String

    Hi,

    as I compile the following code, I dont get an error during compilation, but during execution.

    If I leave the string pointer "char *str1" uncommented, it runs fine.
    My question is: why can't I manipulate this string pointer as a normal array? Is there a way to do so?

    If anyone could give me an explanation or just a link where I could read about, it would be great.

    Thank you in advice.

    Code:
    #include <iostream>
    
    // turns i.e. "1234" in "4321" 
    void reverse(char *p)
    {    
         for(int i = 0, j = length(p)-1; i < j; i++, j--)
         {
                 const char tmp = p[i];
                 p[i] = p[j];
                 p[j] = tmp;
         }
    }
    
    
    int main()
    {
         char *str1 = "aprob";
         char str[][12] = { "Test", "Paul", "Paula", "Hallo Paul!" }; // fine
    
         for (int i = 0; i < 4; ++i)
            {
                reverse(str[i]);
                std::cout << str[i] << std::endl;
                p_reverse(str[i]);
                std::cout << str[i] << std::endl;
            }
    
         reverse(str1); // Error while running!!
    
         system("pause");
         return 0;
    
    }
    Last edited by kapri; 05-01-2005 at 01:59 PM. Reason: reverse(str1); // Error

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Hi,

    Your problem has to do with the way string literals are handled. String literals are anything between quotes, e.g. "some text". When the compiler sees a string literal it slaps a '\0' character onto the end of it like this:

    some text\0

    and stores it in memory, and then returns a pointer to the string. If another part of your code lists that same string literal, e.g:
    Code:
    char* str1 = "some text";
    ...
    ...
    char* str2 = "some text";
    the compiler won't store the duplicate string literal in memory. Instead, it returns a pointer to the first "some text\0' it already stored in memory. After that, if str2 were able to change the string in memory, then str1 would also reflect that change, so the compiler makes the string in memory a constant, and it's full type is const char[]. Therefore, you are not allowed to change the string literal with either str1 or str2.

    The result of all those shenanigans is that there is a type mismatch in the statement:

    char* str1 = "some text";

    The compiler makes the type of the string literal const char[], yet the variable on the left side isn't const. My book says the compiler does not flag that as an error due to C legacy stuff. However, as you found out, you will get a runtime error, and that's because you are not allowed to change the string literal. Since an astute C++ programmer will try to employ the compiler whenever they can to help them catch errors, they will change the code to:

    const char* str1 = "some text";

    If you write it like that, the compiler will flag any attempt to change a string literal at compile time.

    On the other hand, a statement like this:

    char str3[] = "some other text";

    works in a slightly different way. If the string doesn't already exist in memory, then the compiler slaps a '\0' character onto the end of the string literal producing:

    some other text\0

    and stores it in memory. But in this case, the compiler does not return a pointer to the string literal. Instead, the compiler sets aside some memory for str3, and then the compiler strcpy()'s the string literal into str3--character by character including the '\0'. Therefore, str3 doesn't point to a const string literal in memory, and it's perfectly fine to change str3 as much as you want.
    Last edited by 7stud; 05-01-2005 at 04:05 PM.

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Germany
    Posts
    12
    Thank you very much for the rapid and detailed explanation, it helped a lot!

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Your welcome.

    I don't know if you have learned about operator overloading yet, but in C++ you can define exactly what you want '=' to mean for a type. In this case, '=' is defined to mean different things for the types char* and char[]:

    char* str = "some text";

    For the type char*, '=' is defined to get a pointer to the string literal

    char str2[] = "some text";

    For the type char[], '=' is defined to create some memory for str2 and strcpy() the string literal to str2
    Last edited by 7stud; 05-01-2005 at 05:36 PM.

  5. #5
    Registered User
    Join Date
    May 2005
    Location
    Germany
    Posts
    12
    No, I haven't learned operator overloading yet. But thanks for the point, I didn't know that.

    By the way, wich book do you read? I'm reading "The C++ Primer" from Stanley B. Lippman (although 2.ed. 1996 its a little bit old).

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    By the way, wich book do you read?
    "Ivor Horton's Beginning C++"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 07:04 AM
  2. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 04:36 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21