[Info-vax] Significance of 2020 / 2021 date
Stephen Hoffman
seaohveh at hoffmanlabs.invalid
Sat Jan 9 17:11:01 EST 2021
On 2021-01-09 21:38:07 +0000, 1tim.lovern at gmail.com said:
> On Thursday, January 7, 2021 at 2:00:26 PM UTC-7, issinoho wrote:
>> ...
>> if (atoi (date_string + 5) > 2020).
>
> you are adding 5 to the ascii value of the first byte of that date string.
The cited C code above is not adding 5 to the ASCII value of the first
byte. The date_string + 5 syntax shown is performing pointer math, and
moves the begining of the string by five of whatever the pointer is
pointing to. That'll be 5 characters, if date_string is a character
pointer as would be typical.
A word or longword pointer would be somewhat unusual here, though
pointer math works with that, too. Adding 5 would relocate by 10 or by
20 bytes for a word or longword pointer, respectively. But I digress.
If this is a request to add 5 to the ASCII value of the first byte,
then the code would typically be date_string[0] + 5, or maybe
*date_string + 5, assuming a character pointer. date_string[0] and
*date_string both return the byte value for the specified pointer,
again assuming a character pointer.
> code should read
> if ((atoi(date_string) + 5) > 2020)'
That C code will convert the contents of the bytes at the specified
address to an integer, then add 5 to the result.
(Valid input for atoi can include an optional leading + or -, then some
number of decimal digits up to the first non-decimal-digit or the first
null byte. Invalid string input gets a 0 as a return from atoi.)
But without more context to the code, it's difficult to determine the
intent of the programmer with absolute certainty.
Hoff
--
Pure Personal Opinion | HoffmanLabs LLC
More information about the Info-vax
mailing list