View previous topic :: View next topic |
Author |
Message |
Dandy Guest
|
Clearing arrays |
Posted: Thu Feb 03, 2005 9:08 am |
|
|
Hey guys (and gals)
i'm making an embedded device which, all in all, needs to disect packets for strings within. Problem i'm having is that it stores the the text from a packet within an array.. then... over writes the array with the new packet when it comes in. This works fine until i get a short packet.
The array will fill up with say.."dandy likes work"
then when the next packet sent is "meow"
the array now holds "meowy likes work"
i can think of a loop to clear each value back to zero, but is there any quicker way?
Thanks
Dan |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Thu Feb 03, 2005 9:27 am |
|
|
memset() could work for you. But i don't know how it would compare in speed to a loop. |
|
![](templates/subSilver/images/spacer.gif) |
valemike Guest
|
|
Posted: Thu Feb 03, 2005 10:10 am |
|
|
Shouldn't you put the null character after "meow"? Then when you send the string, send only the chars before the null char. |
|
![](templates/subSilver/images/spacer.gif) |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Thu Feb 03, 2005 12:14 pm |
|
|
The standard for strings in C is to terminate them with a null (0x00). Any string processing function stops when it reads a null. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
![](templates/subSilver/images/spacer.gif) |
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Feb 03, 2005 3:38 pm |
|
|
Don't know how you are receiving the data or storing it. But if you are using a pointer and incrementing it after each store, then just assign it a 0 value at that point. Any more data that comes in will overwrite the 0 if no more data comes in then the string is null terminated. |
|
![](templates/subSilver/images/spacer.gif) |
Dandy Guest
|
arrays |
Posted: Sun Feb 06, 2005 8:31 am |
|
|
THANK YOU ALL OF YOU!!!
the origional purpose i was using it for, i can't seem to remember what i was trying to achieve.. or indeed grasp how i am to do it... or maybe i think i may have worked around it... fantastic help though, memset() i'll have to give a try.
MOST IMPORTANTLY
i was having some real trouble with using strstr() (search for string within string).
Before when my code was working with one predefined string, the stycpy(variable,”string”) automatically added the \0 at the end. When i changed the code to work with character arrays, i wasn't including the \0 character, therefore all sorts of problems were happening.
so when i changed the char arrays to all include a delimeter, it all worked hunky dorey, and now i can use variable length arrays.
Cheers guys, you saved me days of struggle and made my final year degree project that much better.
Dan |
|
![](templates/subSilver/images/spacer.gif) |
|