delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/10/26/08:15:24

From: Hans-Bernhard Broeker <broeker AT physik DOT rwth-aachen DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Couple of Routine Questions
Date: 26 Oct 2000 12:04:03 GMT
Organization: Aachen University of Technology (RWTH)
Lines: 75
Message-ID: <8t96jj$ieq$1@nets3.rz.RWTH-Aachen.DE>
References: <39f746ba DOT 108918810 AT 192 DOT 168 DOT 0 DOT 111> <39f7c3ac DOT 971472607 AT 192 DOT 168 DOT 0 DOT 1>
NNTP-Posting-Host: acp3bf.physik.rwth-aachen.de
X-Trace: nets3.rz.RWTH-Aachen.DE 972561843 18906 137.226.32.75 (26 Oct 2000 12:04:03 GMT)
X-Complaints-To: abuse AT rwth-aachen DOT de
NNTP-Posting-Date: 26 Oct 2000 12:04:03 GMT
Originator: broeker@
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019