Thread: still confused with string...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Unhappy still confused with string...

    Hi,guys.

    some days ago I was at a loss with string, and some guys explained it to me. but when I test string stuffs in my compiler I am struck a second time, and I really need any of ur ideas.

    at first, I tested this in my compiler:

    #include <iostream>
    using namespace std;

    int main()
    {
    string s="Hello";
    cout << sizeof(s) << endl;
    cout << s[5] << endl;
    return 0;
    }

    Finally, it input:
    4
    // it input nothing about s[5];

    my question is why the size of my string seemed like this above ?
    the size of s should be 5 not 4, yes ?
    And why it show me nothing with s[5] ? my book said it should be a '\0', it that always invisible ?

    any reply will be helpful,thanx in advance.
    Never end on learning~

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    178
    Okay sorry if i'm wrong about this but I believe with strings it starts with 0 then adds up. So if you have a string of Hello, that would be occupied in the 0, 1, 2, 3, 4. 0 being h, 1 being e and so forth. i'm not sure if this would be causing you're problem or not due to the fact that i'm not too experienced, so if not sorry.

  3. #3
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Correct '\0' is "invisible" or, non-printable.
    You're printing the sizeof a string. I'm not sure on this, but it's most likely a pointer. The output of 4 meant 4 bytes, and a pointer is 4 bytes.

    char string[] = "Hello";
    cout << sizeof(string);

    That should print out 5, as each character is 1 byte.

  4. #4
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Talking

    Dula-Catfish is right. '\0' is non-printable.

    and think this may help:

    /*legal but not good. this prints out nothing*/

    cout << s[5] << endl;

    /*if you want the whole string, just use the identifier without
    the subscripting operator*/

    cout << s << endl;

    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  5. #5
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Unhappy

    Originally posted by Dual-Catfish
    Correct '\0' is "invisible" or, non-printable.
    You're printing the sizeof a string. I'm not sure on this, but it's most likely a pointer. The output of 4 meant 4 bytes, and a pointer is 4 bytes.

    char string[] = "Hello";
    cout << sizeof(string);

    That should print out 5, as each character is 1 byte.
    char string would show me a size of 5 but string one would be 4 ?
    I'm even confused now.
    Never end on learning~

  6. #6
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Question

    Ummm... Black I think that really should print 5 bcoz it is H-e-l-l-o, at 1 byte per character.

    Check your code again and report back any confusing "somethings" again.

    I'll try your code at home too (no compiler here at the cafe)

    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  7. #7
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Unhappy

    Originally posted by billholm
    Ummm... Black I think that really should print 5 bcoz it is H-e-l-l-o, at 1 byte per character.

    Check your code again and report back any confusing "somethings" again.

    I'll try your code at home too (no compiler here at the cafe)

    I tried tens of times but the same result returns..........

    and something surprised me that I got a size of 6 when I rewrite the string s="Hello"; to char s[]="Hello";.................

    both size are not 5 ? I got much much more confused now......
    Never end on learning~

  8. #8
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Question

    Black what's your compiler?

    Compilers sometimes produce unforeseen bugs that muddles your program even if your code is goddam right.

    It happened to me when I used Turbo C 2.0: told me there was a syntax error because of a missing colon in my switch() block even though it was complete. When I compiled it using Turbo C++ 1.01 and Borland C++ 3.1, it worked just fine

    So... try another compiler and report your progess
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  9. #9
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    I wrote the code with Dev-C++, a very convenient one. but to confirm my code to be correct I would test it with my BCB when back home. cross my fingers.
    Last edited by black; 05-20-2002 at 05:37 AM.
    Never end on learning~

  10. #10
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    char s[] = "Hello";
    is creating an array large enough to hold the string
    H E L L O \0, automatically appending the null terminator for your string. This means the array s is 6 bytes, with an H at byte 0, and a null at byte 6.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  11. #11
    Registered User
    Join Date
    Aug 2001
    Posts
    101
    When you want to find the size of a std::string, use size():
    Code:
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        string s = "Hello, world!";
        cout << s.size() << '\n';
    }
    When you need the size of nul-terminated string (C-style), use strlen():
    Code:
    #include <cstring>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        const char* s = "Hello, world!";
        cout << strlen(s) << '\n';
    }
    You get unexpected results in your code, because you misunderstand what sizeof does. It gives the size of an object (or pointer) in bytes, not the length of a string. For example, if int is 32 bit and a byte is 8 bits on a particular machine, then sizeof(int) == 4.

    On many systems a pointer is 4 bytes. std::string is often implemented like this:
    Code:
    // It is actually template<typename E, typename CharT = char_traits<E> > class basic_string
    // but do not worry about that right now
    class string
    {
        struct Data
        {
            char* end;
            char* endOfMem;
            char buf[1];
        };
        Data* data_;
    
    public:
        // public implementation...
    };
    This means that a string object contains exactly one pointer, which makes its size 4. That is why in your example, sizeof s == 4.

    The only place where sizeof can return the length of a nul-terminated string is when it is an array:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char s[] = "Hello, world!";
        cout << sizeof s - 1 << '\n';
    }
    In this case, s is an array of chars, so sizeof knows the actual size of the array. However, you will get one character more, since sizeof counts the terminating '\0' as well, so you just subtract one and get the correct result.

    In conclusion, when you need the length of a std::string use its size() member function and when you need the length of a nul-terminated string use strlen. Furthermore, invest in some good books because these are the basics and you should understand them well.
    - lmov

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    249

    I think ....

    hmmmmmmmmmmmmmm....

    Do you have Windows XP.
    if yes ... it is ok


    --------- But I don't know ...
    LQQK try this
    Code:
    int x = s.size();
    cout<< x<< endl;
    cout<<"  I don not like that " << endl;
    s=  " This is not fare";
    cout << s.size()<< endl;
    cout<< "how about now???"
    C++
    The best

  13. #13
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225

    Talking Tololot!!! Tololot!!!

    Come to think of it... maybe Azuth is right.

    I always thought that we were after the length of the string but since Black's code used sizeof(), it would print out the size of the string in bytes.

    I'll try that at home.
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  14. #14
    Registered User
    Join Date
    May 2002
    Posts
    49
    Imov is right. That is.
    Hello, everyone.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM