[Xastir] Peet Bros U500 w/xastir

Alex Carver kf4lvz at yahoo.com
Tue Sep 4 23:42:59 EDT 2007


--- Rick Green <rtg at aapsc.com> wrote:
>             /* wind speed */
>              if (data[2]!='-') { // '-' signifies
> invalid data
>                  substr(temp_data1,(char
> *)(data+2),4);
> 
>   ...so here's how I'm reading this:  The input line
> is in an array of 
> characters 'data', and data[2] would refer to this
> array at offset 2. 
> Bytes 0 and 1 would contain the identifiers '!!', so
> wind speed would be 
> in the four bytes beginning at offset 2.  Here's
> where my C ignorance 
> shows.  I don't understand how you can refer to the
> same string as an 
> array of bytes (data[2]), and also in the aggregate,
> as you are using a 
> substring function to pick our four consecutive
> bytes starting at data+2, 
> which is the same location as data[2]?
> 

Nice work on the latter part fixing the wind direction
(I'm not a developer but it's certainly a good catch).
 It might be that the code was borrowed from an older
version of the Peet protocol and no one noticed.

As for array of bytes/strings, in C there isn't really
a string.  It's an array of characters that have
loosely been called a string.  So data[2] is a single
character and will return only one character.  Data+2
is actually an address, not a character.  That's why
it's used in the substr() function: substr() is asking
for an array of characters, a start address and a
length.

To do what substr() is doing by using arrays, you'd
have to create an array of characters four bytes long
and then copy the data in like this:

char temp_data1[4]; /* array of char four bytes long
*/

temp_data1[0] = data[2];
temp_data1[1] = data[3];
temp_data1[2] = data[4];
temp_data1[3] = data[5];

Not as pleasant as just:

substr(temp_data1,(char *)data+2,4)

Also, the (char *) bit ensures that the value returned
by "data+2" is cast as a pointer (specifically to a
character).  Mainly you just want to be sure you're
getting a pointer to a memory location which is what
substr wants.


       
____________________________________________________________________________________
Got a little couch potato? 
Check out fun summer activities for kids.
http://search.yahoo.com/search?fr=oni_on_mail&p=summer+activities+for+kids&cs=bz 



More information about the Xastir mailing list