CFanatic

Go Back   CFanatic > Programming > C Programming

Join CFanatic Forum Now

C char array help! topic posted under C Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 11-19-2007, 09:31 PM
Junior Member
 
Join Date: Nov 2007
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Dylan is on a distinguished road
| More
C char array help!

Well, here is the protocol I'm trying to match:

Code:
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)

Code:
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); 
    sprintf(newarray,"%s%s%s",begin,MOTD,end);

    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);

    printf("Sent!");
}
Forgive the crappy coding style, I've tried chopping this up so many times and fixing it again, to no avail.

Kind Regards,
Dylan
Reply With Quote
  #2  
Old 07-31-2008, 04:45 PM
Senior Member
 
Join Date: Jul 2008
Posts: 101
Thanks: 0
Thanked 0 Times in 0 Posts
xpi0t0s is on a distinguished road
| More
Re: C char array help!

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.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
1-dimensional array type double.....and....2-dimensional array styles8687 C++ Programming 2 01-01-2009 04:30 AM
Reset array coderzone C# Programming 1 03-11-2008 11:25 PM
Help with parallel array psppb C++ Programming 3 12-31-2007 07:14 AM
Using gdb how to print multibyte or wide char data. rajeshgalla C Programming 0 10-18-2007 08:10 AM
multi byte to wide char converting problem(Related to Unicode) rajeshgalla C Programming 0 10-10-2007 09:00 AM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 03:10 AM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board