"raw" strings

This is a discussion on "raw" strings within the C Programming forums, part of the General Programming Boards category; Hello! It is common for me to implement integration between systems that communicate through socket. To perform this, I must ...

  1. #1
    Registered User Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152

    "raw" strings

    Hello! It is common for me to implement integration between systems that communicate through socket. To perform this, I must fill structures following a specific protocol, for example: a system that informs the situation of a client, given its id, would have this struct:
    Code:
    struct Client {
        char protocol_version[5];
        char client_id[5];
    };
    The protocol_version is constant, so I want to make it static to fill it, for example, with the string "01.27". I only know one, bad way, to do it. After declaring protocol_version to be static I would write:

    Code:
    char Client::protocol_version[] = {'0','1','.','2','7'};
    When, in fact, I would like to write:

    Code:
    char Client::protocol_version[] = "01.27";
    The problem is the implicit '\0', that overflow the fixed size of the field. I could use sprintf, or strcpy, writing the '\0' in the next field, but this seems so flawed. Is there a solution?

    Thanks in advance. (I could have just asked: is there a way to write "01.27" without trailing '\0' :P)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    Well that's one of the differences between C and C++.

    What you're trying to do would be perfectly legal C, but it illegal in C++ (BTW, check your forum before posting).

    You could use strncpy(), which won't copy the \0.
    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 Mortissus's Avatar
    Join Date
    Dec 2004
    Location
    Brazil, Porto Alegre
    Posts
    152
    Well, if I am going to use strncpy would not be best to use memcpy instead?

    The question remains the same, although I agree that I have posted in the wrong forum, sorry. Can I attribute the fixed value staticaly?

    Thanks!

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I could say memcpy() would be better in this case, if you're not going to include a '\0', although I would suggest you keep the '\0' (for debugging and such...), but just not write the '\0' to the socket.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    What you've written doesn't seem to be valid code, in either C OR C++. But the following is valid in C:

    Code:
    const char version[5] = "12.34";
    When the compiler sees that, it leaves off the trailing '\0' byte. But it won't work in C++ (which is what you seem to be using, given that you have that :: operator in there)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 10:34 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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