string question

This is a discussion on string question within the C++ Programming forums, part of the General Programming Boards category; Hello.. Is this: Code: char *query = new char[512]; char *ptr = query; *ptr++ = 0; *ptr++ = 15; *ptr++ ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    string question

    Hello..

    Is this:

    Code:
    char *query = new char[512];
    char *ptr = query;
    *ptr++ = 0;
    *ptr++ = 15;
    *ptr++ = 0;
    *ptr++ = 1;
    the same as:

    string query = "01501";

    ?

    If not, how should I define string query then?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    No it's not the same.

    Try strcpy( query, "01501");
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    151
    Seems to me it'd be more along the lines of memcpy(query, "\000\017\000\001", 4)
    System: Debian Sid and FreeBSD 7.0. Both with GCC 4.3.

    Useful resources:
    comp.lang.c FAQ | C++ FQA Lite

  4. #4
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    i'd be more along the lines of

    std::string = "01501";

  5. #5
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    The problem is I just want to have c++ string the same as c string and I wonder what would be the right way, since c++ strings are better to use and are not NULL terminated.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    Just use a std::string for everything.

    When you absolutely need the c-string equivalent, because that's what some API function expects, then just use mystring.c_str() to get a const char * equivalent.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    So would it be okay to do std::string str = "01501"; for what I want? Or would it be better to use vector <char> instead?

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,442
    Or a vector of ints
    It seems like you're not storing strings at all, so stop writing things between double quotes.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Its not actually a string, its a data that will be sent in binary mode through socket.

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Do you know <bitset>? If not, check it out. It's got lots of binary functionality.

  11. #11
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by twomers
    Do you know <bitset>? If not, check it out. It's got lots of binary functionality.
    I have no idea how I should use it in this case..

  12. #12
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    It's very handy for int-bit conversion and whatnot. Check it out. I'm sure I have mentioned it before on the forums.

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. Reusing a string pointer question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2009, 04:53 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 05:44 PM

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