Mail Archives: djgpp/2000/10/26/08:15:24
Kenneth Lantrip <bothersome AT mindspring DOT com> wrote:
> Looks kinda funny to reply to my own message but... here goes :)
Esp. if one considers that this one is written with a completely other
user name on it :-P
As a side issue: I suggest you take this over to comp.lang.c or some
other general C newsgroup. This is in no way specific to DJGPP, so it
doesn't really belong into this newsgroup.
> #include <stdio.h>
Somewhere around here, you should add prototype declarations for your
functions Bin2Int and Int2Bin, so the compiler knows what parameter
types they take, and what their result type is.
> main(void) {
[Nitpick: make that "int main(void)". Not specifying the return type
at all is deprecated usage of C, and is no longer tolerated by the new
C99 language definition.
> int x;
> static unsigned char y[33];
You don't need that 'static', in this case.
> x = 42;
> printf(" %s ", Int2Bin(x, &y));
> /* someone tell my why that worked!!! */
You got lucky, that's way. This is seriously incorrect code, which
invokes what the C language definition calls 'undefined behaviour',
i.e. *anything* can happen. In this case, the expected thing happened,
by sheer luck.
Had you compiled with a proper set of warning options ("gcc -O2 -Wall
-W" is what I would suggest), you'ld have got a warning about this.
Correct code would've looked like this:
Int2Bin(x, y);
printf(" %s ", y);
> strcpy(y, "1011101011");
> printf(" %d \n", Bin2Int(y));
You don't have to write Bin2Int yourself. There's a standard function,
'strtol' that does it for you.
> }
> int Int2Bin(int x, char *y) {
> int i;
>
> y += 32; *y-- = 0;
> for (i = 0; i < 32; i++) {
> *y-- = 48 + (x & 1);
> x >>= 1;
> }
Bad coding: the promised integer is not return()ed, here. It's
probably better to return char *, anyway.
> }
> I've discovered it would make other stuff a lot easier if all my
> returned 1's and 0's were all the same length (like 32 of em).
--
Hans-Bernhard Broeker (broeker AT physik DOT rwth-aachen DOT de)
Even if all the snow were burnt, ashes would remain.
- Raw text -