1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C char array help!

Discussion in 'C' started by Dylan, Nov 20, 2007.

  1. Dylan

    Dylan New Member

    Nov 20, 2007
    Likes Received:
    Trophy Points:
    Well, here is the protocol I'm trying to match:

    14 12 00 [string messageoftheday] 2E 0A 64 [UInt16 world] [string worldip] [UInt16 port]
    And the world, worldip, port is all constant in this case. So, I wanted to make 2 constant char arrays, beginning array being "0x14, 0x12, 0x00" and the end array being "2E 0A 64 [UInt16 world] [string worldip] [UInt16 port]"

    Now, when I tried doing this, and making it add in the string (remembering that the string has to have it's own length in front of it as a byte) it decides to crash. I'm horribly newbish at C, so a little help could be useful.

    Here is what I have so far: (i'm using a set motd just for an example, so one of you people would be able to compile and check what is wrong with it)

    int SendMotd(const char MOTD, SOCKET ClientSockett)
        char begin[] = { 0x14, 0x1C, 0x00};
        char motd[] = { 0x60, 0x57, 0x65, 0x6C, 0x63, 0x6F, 0x6D, 0x65, 0x20, 0x74, 0x6F, 0x20, 0x74, 0x68, 0x65, 0x20, 0x61, 0x75, 0x74, 0x75, 0x6D, 0x6E, 0x20, 0x75, 0x70, 0x64, 0x61, 0x74, 0x65, 0x20, 0x32, 0x30, 0x30, 0x37, 0x21, 0x20, 0x53, 0x65, 0x65, 0x20, 0x77, 0x77, 0x77, 0x2E, 0x74, 0x69, 0x62, 0x69, 0x61, 0x6D, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x20, 0x6F, 0x72, 0x20, 0x77, 0x61, 0x70, 0x2E, 0x74, 0x69, 0x62, 0x69, 0x61, 0x6D, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x20, 0x66, 0x6F, 0x72, 0x20, 0x6D, 0x6F, 0x72, 0x65, 0x20, 0x69, 0x6E, 0x66, 0x6F, 0x72, 0x6D, 0x61, 0x74, 0x69, 0x6F, 0x6E };
        char end[] = { 0x2E, 0x0A, 0x64, 0x0F, 0x00, 0x0E, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x30, 0x2E, 0x30, 0x30, 0x30, 0x2E, 0x30, 0x30, 0x31, 0x6E, 0x00 };
        char newarray = malloc((sizeof(char) * (strlen(begin) + strlen(motd) + strlen(end))) + 1); 
        char leng[] = { sizeof(newarray) };
        char leng2[] = { 0x00 };
        send(ClientSockett, leng, sizeof(leng), 0);
        send(ClientSockett, leng2, sizeof(leng2), 0);
        send(ClientSockett, begin, sizeof(begin), 0);
    Forgive the crappy coding style, I've tried chopping this up so many times and fixing it again, to no avail.

    Kind Regards,
  2. imported_xpi0t0s

    imported_xpi0t0s New Member

    Jul 18, 2008
    Likes Received:
    Trophy Points:
    String functions REQUIRE a NULL terminator, and they REQUIRE the null terminator to be at the end.
    So strlen(motd) won't work because there's no null terminator, and strlen(end) won't work because there's an embedded NULL (the fifth value in the array).
    I'm not sure what you expect strlen(begin) to return but it will count two characters, then find the terminating NULL so return 2. Are you expecting 3?

    sizeof does what I think you're expecting though (tested in Visual Studio 2005), sizeof(begin)=3, motd 95 and end 22.
    So you could use memcpy instead of strcpy, since the data you're dealing with isn't string data. sprintf won't work at all because it also works with strings, not generic binary data. Don't bother with strncpy because this also looks for terminating NULLs, if you tell it to copy all 22 bytes of end[] it will still stop after 4 bytes.

    > char newarray = malloc...

    newarray needs to be a pointer (char*). char *newarray=malloc...
    I think the Standard defines sizeof(char) always to be 1, so "sizeof(char)*" is unnecessary. Although that might be the C++ standard I'm thinking of.

    > char leng[] = { sizeof(newarray) };

    Er, no idea what you think this'll do. If it works at all leng[] will be a single byte "array" containing the size of a pointer, which is probably 4. Do you mean char *leng=malloc(sizeof(newarray));? sizeof() is computed at compile-time, it's not a runtime function, so sizeof(char*) will always be the size of a pointer no matter what it's pointing at.

    If you're dealing with binary data in char arrays then it might be worth associating the data with a length field one way or another. e.g. struct bindata { char *data; int len; } then anything that updates data must also update len.

Share This Page