delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/10/26/12:31:39

From: bothersome AT mindspring DOT com (Kenneth Lantrip)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Couple of Routine Questions
Date: Thu, 26 Oct 2000 14:33:59 GMT
Organization: Home
Message-ID: <39f93e55.172314770@192.168.0.111>
References: <39f746ba DOT 108918810 AT 192 DOT 168 DOT 0 DOT 111> <39f7c3ac DOT 971472607 AT 192 DOT 168 DOT 0 DOT 1> <8t96jj$ieq$1 AT nets3 DOT rz DOT RWTH-Aachen DOT DE>
X-Newsreader: Forte Agent 1.5/32.452
MIME-Version: 1.0
NNTP-Posting-Host: shv6-170.shreve.net
X-Trace: 26 Oct 2000 09:41:47 -0500, shv6-170.shreve.net
Lines: 88
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

In an article posted to usenet, Hans-Bernhard Broeker
<broeker AT physik DOT rwth-aachen DOT de> inscribed the following:

>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

I forgot to change my name back...  Not real sure how safe using my real name.

>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.

OK, I see more study involved.

>> 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.

OK, I'll start doing that.  That was something I wasn't aware of.

>>     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);

That's what I had started with, but after experimenting with what works and
what doesn't, It just looked better "the wrong way".

>>     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.

Isn't that the way it always works?!? :)

>> }

>> 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.

Ahhh...  Good to know this.

>> }

I appreciate your help...  I've got 4 books on the subject, and none of them
had these jewels of info in them.  But then I don't read them from front to
back either.

- Raw text -


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