memmove
pilot0315
Posts: 913
Howdy folks, thanks for the help on the other problem. Got it solved. I am presently using memmove to parse out my gps tokens so as to move the data around.
First using the time. I can move the hrs to hrs, but when I move over two to get the min it gives me the seconds. Is there something special about memmove??
starts at line 150
Thanks
First using the time. I can move the hrs to hrs, but when I move over two to get the min it gives me the seconds. Is there something special about memmove??
starts at line 150
Thanks
Comments
Another problem I see is that you are not terminating your strings with a null byte. A 2-character string should be 3 bytes long, where the third byte is 0.
Finally, the function strtok will return a value of zero when there are no more tokens available. To make your code tolerant to errors in the input string, you should test for a zero return value to determine if the token has been successfully retrieved. Of course, if you can guarantee that the input string is always formatted correctly you don't have to worry about testing for a null pointer.
If I make gpstime a char I get this if I make it an int it works fine
%s requires a null-terminated string (ie, ends with a zero byte). Since your struct just contains six characters in a row, followed immediately by other stuff, the print function has no idea where to stop.
You could add a 7th character to gpstime and zero it. In fact, since you're using strcpy() to copy the string into gpstime, it's already doing the null termination for you, overwriting the first value in the next struct member with a zero. Since you fill in the "fix" value immediately after gpstime, you don't notice this.
Anywhere you're using strcpy you need to make sure your destination buffer has room for the extra terminating character, so your struct should probably look like this:
Even that isn't completely safe because if your source string isn't well formatted, you could overrun your copy. For example, if you ended up with a missing comma in the source string, your string copy wouldn't terminate where you thought it would, and you'd copy more data than expected. There are versions of string copy that take a "maximum length" value that are safer, or, safest of all, if you know it's 6 characters, and it's ALWAYS 6 characters, just do this:
You could also print it as six chars instead of a string, like this:
...but that doesn't fix your "null terminate" issue from using the string copy functions.
I am going to try your latest suggestion.