decimal to binary function error


 
Thread Tools Search this Thread
Top Forums Programming decimal to binary function error
# 1  
Old 12-20-2007
decimal to binary function error

I have the following simple code to return a binary number in a array format given an interger and the number of the bits for specifying the interger as binary number.

#include <stdio.h>
#include <stdlib.h>

int main ()

{
// int* get_binary_number(int* bit_array, int num, int bit_count);
int* bitcount ( int n, int bit_count) ;
int* bit_array;
int i;

// bit_array = get_binary_number(bit_array, 23, 6);
bit_array = bitcount(23, 6);
printf("\nUsage: \n");
printf("\n the number '23' in binary is ");

for (i=0; i<6; i++)
{
printf("%d", bit_array[i]);
}


return 0;
}

int* bitcount ( int n, int bit_count)
{
int count[bit_count];
int bit_place = bit_count - 1;
while (n)
{
count[bit_place] = n && 1 ;
n >>= 1 ;
bit_place--;
}
return count ;
}

this program does compile with a warning

gcc -c -Wall -c -o test.o test.c
test.c: In function bitcount':
test.c:36: warning: function returns address of local variable
gcc test.o -o test


But when I try to run it, there is no result.
Any help to fix this is greatly appreciated.

Thank you,
# 2  
Old 12-20-2007
Quote:
Originally Posted by return_user
test.c: In function bitcount':
test.c:36: warning: function returns address of local variable
Did you read the warning?

Add the command line option "-Werror" and it will be a bit clearer.
# 3  
Old 12-21-2007
Hi Porter

So what is the best way to return the binary number as a array of integers?

Thanks,
# 4  
Old 12-21-2007
Quote:
Originally Posted by return_user
So what is the best way to return the binary number as a array of integers?
You can't return arrays in C, but you can

(a) return a pointer to array elements, hence has to abide by the memory allocation rules, eg not being on the stack of the function that just returned.

(b) define a struct that contains the array, then return the struct.
# 5  
Old 12-21-2007
Hi porter,

I changed my program to this
#include <stdio.h>
#include <stdlib.h>

int main ()

{
// int* get_binary_number(int* bit_array, int num, int bit_count);
int* bitcount ( int* count, int n, int bit_count) ;
int* bit_array;
int i;
int temp_array[6];

// bit_array = get_binary_number(bit_array, 23, 6);
bit_array = bitcount(temp_array,23, 6);
printf("\nUsage: \n");
printf("\n the number '23' in binary is ");

for (i=0; i<6; i++)
{
printf("%d", bit_array[i]);
}


return 0;
}

int* bitcount ( int* count, int n, int bit_count)
{
//int count[bit_count];
int bit_place = bit_count - 1;
while (n)
{
count[bit_place] = n && 1 ;

n >>= 1 ;
bit_place--;
}
return count ;
}

and don't get any warnings/error during compilation,
63 memgen > make
gcc -c -Wall -Werror -c -o test.o test.c
gcc test.o -o test
64 memgen >
64 memgen > test
65 memgen >

but there is no output on the screen as well.
What could be wrong?

Thanks
# 6  
Old 12-21-2007
Code:
#include <stdio.h>
#include <stdlib.h>

int* bitcount ( int * count, int n, int bit_count) 
{
	/* int count[bit_count]; */
	int bit_place = bit_count - 1;

	while (bit_count--)
	{
		count[bit_place] = (n & 1) ;

		n >>= 1 ;
		bit_place--;
	}

	return count ;
}

int main (int argc, char **argv)
{
	/* int* get_binary_number(int* bit_array, int num, int bit_count); */
	int* bitcount ( int* count, int n, int bit_count) ;
	int* bit_array;
	int i;
	int temp_array[6];

	/* bit_array = get_binary_number(bit_array, 23, 6); */

	bit_array = bitcount(temp_array,23, 6);

	printf("\nUsage: \n");
	printf("\n the number '23' in binary is ");

	for (i=0; i<6; i++)
	{
		printf("%d", bit_array[i]);
	}

             printf("\n");

	return 0;
}

BTW, don't use // in C.

Last edited by porter; 12-21-2007 at 04:52 PM..
# 7  
Old 12-21-2007
Hi porter,

Thank you for the prompt help but I still don't get any print messages on the stdout.
How do I know that its working right?

Thanks,Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Negative decimal to binary

Is there a fast way to convert a negative decimal value into a signed binary number in bash script ? I've looked a lot on internet but I saw nothing... (For exemple : -1 become 11111111.) (9 Replies)
Discussion started by: Zedki
9 Replies

2. Programming

Urgent help needed.. C++ program to convert decimal to hexa decimal

Hi , seq can be 0...128 int windex = seq / 8; int bindex = seq % 8; unsigned char bitvalue = '\x01' << (7-bindex) ; bpv.bitmapvalue = bitvalue; This is the part of a program to convert decimal to bitmap value of hexadecimal. I want this to change to convert only to... (1 Reply)
Discussion started by: greenworld123
1 Replies

3. Programming

Binary to decimal for particular bits

Hello, I have script which work fine on particular data.file . The next feature I want to achieve is to get the decimal equivalent of data to data. The data looks like this : data(01000000000000000000110000000000) thank you.. #include <iostream> #include <fstream> #include... (4 Replies)
Discussion started by: emily
4 Replies

4. Shell Programming and Scripting

How to get the negate of decimal to binary?

Hi All, New to this forum (and yes , a newbie in programming..:p) I have a decimal to binary converter script done this way : i=$1 bit0=$(( (i & 0x01) > 0 )) bit1=$(( (i & 0x02) > 0 )) bit2=$(( (i & 0x04) > 0 )) bit3=$(( (i & 0x08) > 0 )) bit4=$((... (6 Replies)
Discussion started by: digiteltlc
6 Replies

5. Homework & Coursework Questions

Decimal to BCD (Binary Coded Decimal)

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Design an algorithm that accepts an input a decimal number and converts it into BCD (Binary... (2 Replies)
Discussion started by: caramba
2 Replies

6. UNIX for Dummies Questions & Answers

Decimal to BCD (Binary Coded Decimal)

Anybody please help me... Design an algorithm that accepts an input a decimal number and converts it into BCD (Binary Coded Decimal) representation. Also, draw its Flow Chart. This is a unix qn... plz post algorithm for that :confused: (1 Reply)
Discussion started by: caramba
1 Replies

7. Programming

Binary conversion function

Is/are there any function(s) in C that convert(s) character/ASCII/Decimal to binary and vice versa? what about bcopy and strcpy? (1 Reply)
Discussion started by: Peevish
1 Replies

8. Shell Programming and Scripting

unable to return a decimal value from a function

Hi Guys, I am unable to return a decimal value from a function to the main script. I am getting the correct value in the function but when it is returning to the main script using "return" it is coming as 0. Below is my code. read VALUE_V fun_FATHERID fun_SONID fun_TOPID fun_RTYPE <... (2 Replies)
Discussion started by: mac4rfree
2 Replies

9. UNIX for Advanced & Expert Users

Converting Binary decimal coded values to Ascii Values

Hi All, Is there any command which can convert binary decimal coded values to ascii values... i have bcd values like below 0-- -v - Pls suggest a way to convert this. Thanks, Deepti.Gaur (3 Replies)
Discussion started by: gaur.deepti
3 Replies

10. Shell Programming and Scripting

unix script for converting a decimal to binary

Could anybody please help me in writing a script in unix for converting a decimal number to binary number. (3 Replies)
Discussion started by: softy
3 Replies
Login or Register to Ask a Question