Can't find the bug

This is a discussion on Can't find the bug within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <string.h> int bin2dec(const char *); char* dec2bin(const char *); int main(void) { char dec[33]; ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    204

    Can't find the bug

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int bin2dec(const char *);
    char* dec2bin(const char *);
    
    int main(void)
    {
    	char dec[33];
    	char bin[33];
    
    	printf("Type a binary number: ");
    	fgets(bin, sizeof(bin), stdin);
    	printf("%s is %d in decimal\n", bin, bin2dec(bin));
    
    	printf("\nType a decimal number: ");
    	fgets(dec, sizeof(dec), stdin);
    	printf("\n%s is %s in binary\n", dec, dec2bin(dec));
    
    	return 0;
    }
    
    int bin2dec(const char *binario)
    {
    	int i = 0;          
    	char *ptr;   
    	int dec = 0;
    
    	if((ptr = strchr(binario, '\n')) != NULL)
    		*ptr = '\0';
    
    	while(binario[i] == '0' || binario[i] == '1' && i <= 32)
    	{
    		if(binario[i] == '0')
    			dec <<= 1;
    
    		else
    		{
    			dec ^= 1;
    			dec <<= 1;
    		}
    
    		++i;
    	}
    
    	dec >>= 1;
    
    	return dec;
    }
    
    char* dec2bin(const char *decimal)
    {
    	int i;        
    	int dec;      
    	int length;   
    	char *ptr;   
    	char bin[33];
    
    	if((ptr = strchr(decimal, '\n')) != NULL)
    		*ptr = '\0';
    
    	dec = atoi(decimal);
    
    	i = 0;
    	while(dec)
    	{
    		if((dec & 1) == 1)
    			bin[i] = '1';
    
    		else
    			bin[i] = '0';
    
    		dec >>= 1;
    		++i;
    	}
    
    	bin[i] = '\0';
    
    	length = strlen(bin) - 1;
    
    	/* reverse array in place */
    	for(i = 0; i < length; i++, length--)
    	{
    		   /* I use dec as a temporary varible so
    		      that I don't have to declare another one */
    		dec = bin[i];
    		bin[i] = bin[length];
    		bin[length] = dec;
    	}
    
    	return bin;
    }
    Here's what happens when I run it:
    Code:
    Type a binary number: 1010
    1010 is 10 in decimal
    
    Type a decimal number: 10
    
    10 is hÉ@ in binary
    Can someone help me find what is wrong? Thank you.

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Code:
    char* dec2bin(const char *decimal)
    {
    	<snip>
    	char bin[33];
    	<snip>
    	return bin;
    }
    Don't return pointers to local variables. bin is already out of scope by the time you try to use it.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    I declared bin as static and it worked. Is it okay to do such a thing? Thanks.

  4. #4
    Registered User
    Join Date
    May 2005
    Posts
    24
    definately not a good idea. just add an extra parameter to the function for a destination buffer.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Quote Originally Posted by caduardo21
    I declared bin as static and it worked. Is it okay to do such a thing? Thanks.
    That's one way to go about it. There's nothing wrong with doing it that way.
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    24
    well, you just have to be sure to do something with the data between calls.

  7. #7
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Quote Originally Posted by legend
    well, you just have to be sure to do something with the data between calls.
    like strncpy() to a local variable. Though I believe it would be easier just to send a char * and just edit the string directly.
    Last edited by Kleid-0; 05-16-2005 at 05:49 PM.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by itsme86
    That's one way to go about it. There's nothing wrong with doing it that way.
    I wouldn't say nothing. If that same function suddenly has to be executed in a threaded environment, you will get problems.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I would. There's nothign wrong with using the static keyword. You just have to understand how it works. If you understand how it works, then you're fine in using it.

    There's no reason to use a sledge hammer to drive a nail when a simple claw hammer will work better. Use the tool that fits the task.

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by quzah
    I would. There's nothign wrong with using the static keyword. You just have to understand how it works. If you understand how it works, then you're fine in using it.
    well, when code at the time of writing is not multithreaded you wont have any problems. But when a few years later the same function has to run in a multithreaded environment (I have seen it happening) this will be one extra thing to change. So it's more a practical maintenance issue as there is nothing wrong with using local static return values from a C standard point of view.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Does this homework problem really look like it's going to be multithreaded to you? Ever?

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by quzah
    Does this homework problem really look like it's going to be multithreaded to you? Ever?

    Quzah.
    Saying there's nothing wrong with using static doesn't help either.

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    No need for fighting guys, the problem's solved. I changed the function and both are returning ints now

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Not every function ever written has to be made for a multi-threaded enviornment.

    It comes down to one simple question: Do you know how to correctly use the function you've written? If no, you'd better rewrite it. If yes, then where's the problem?

    This function isn't the end all function of all functions. It does one task, and the person that wrote it knows how to use it. Who gives a ........ if it doesn't work in some other case?

    You just apparently have some need to prove that you're "right", so you come up with a scenario where this function won't work. Who cares if it doesn't work in a multi-threaded application. Is this program multi-threaded? No. Does the function work here? Yes. Good. Stop your whining. No one cares if it works in a multi-threaded enviornment except you. In which case, write you own and shut up about it.

    It is a good idea, if they know how to use it, which they do. Just because it's not your idea, doesn't mean it's not a good one. It works. It's perfectly valid C. It conforms to the standard, doesn't invoke undefined behavior, and does the job. That's considered good. That's also considered way better than most of the people posting here.

    There is nothing wrong with using static. You're an idiot if you think there is. I guess you've never heard of strtok eh?


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by quzah
    There is nothing wrong with using static. You're an idiot if you think there is. I guess you've never heard of strtok eh?

    Quzah.
    yeah, strtok is one of the reasons that compilers like VC and BorlandC had to invent stuff like including libcmt.lib or if you did normal C libc.lib. With strtok C started to really show its age. Many people have had crashes in their programs because they used strtok correctly but failed to include the proper crt lib.

    No need to get so defensive

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 07:52 AM
  2. Can't Find Bug in basic MP3 Sorter
    By trickeastcs in forum C++ Programming
    Replies: 12
    Last Post: 12-14-2007, 04:31 PM
  3. Replies: 5
    Last Post: 04-16-2004, 01:29 AM
  4. Time for another round of: FIND THAT BUG!
    By DeepBlackMagic in forum C++ Programming
    Replies: 0
    Last Post: 10-29-2003, 04:29 AM
  5. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21