|
At 14:27 2003-11-17 -0500, Gene_Gaunt@xxxxxxxxxxxxxxx wrote:
>
> Thanks for help with the carry byte, I'm trying this:
>
> void addstring( char result[], char increment[], int length )
> { int x, sum = 0;
> for( x = length-1; x >= 0; x-- )
> { result[x] = sum += result[x] + increment[x];
> sum >>= 8;
> }
> }
>
I hesitate to write this, because
(a) I am a relative newcomer to C programming, writing more
from what I have read than from experience.
(b) I really should not write anything for public
consumption this late at night.
Hmm, maybe those reasons should be listed the other way
around <grin/>. Anyway, I think your code may have a couple
of problems.
result[x] and increment[x] are each a char, and their sum is
a char, which does not have room for the carry. Try coding
the sum as (int) result[x] + increment[x]. Casting either
operand to (int) will make the sum an (int).
To inhibit sign extension during promotion, declare or cast
result and increment to be unsigned char[].
I do not know if the result of narrowing during assignment
to a signed type is well defined. Anyway, declaring or
casting result to be unsigned char[] avoids this suspected
problem, too.
Your loop does not terminate, at least not gracefully, if x
is unsigned. Possible remedies are to declare x to be
signed or to rewrite the loop control as ...
for ( x = length; x > 0; )
{ --x;
...
These are things that caught my attention, looking at the
code. Can somebody better acquainted with the C standard
comment?
HTH.
Terry.
Available for contract programming.
As an Amazon Associate we earn from qualifying purchases.
This mailing list archive is Copyright 1997-2025 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].
Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.