473,804 Members | 2,271 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help - pointers

Hi There,
I'm writing a program that takes a decimal number and returns it's
binary equivalent.
the problem is that I don't know how to return (from function) the
binary value as an array of cocatenated characters. For example : I
want to convert number 3 using 4 bits, ie 3 <----"0011".
After executing, I'm getting this warning:
warning: return from incompatible pointer type
line:49: warning: function returns address of local variable

Here it is my code:

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

int power (int base, int m);
char* dec2bin(int input, int m);

int main ()
{
int input = 0;
int m = 0;

printf("Number to convert\n");
scanf("%d", &input);
printf ("Number of bits\n");
scanf ("%d",&m);
printf("input %d is converted to %s\n",input,dec 2bin(input, m));
exit (0);
}

char* dec2bin(int input, int m)
{
static int i = 0;
char* array[m+1];
int limit = 0;
limit = power(2, m);
const int MASK = limit/2; // the binary equivalent is: 1 + m 0 bits

array[m] = '\0';
for (i= 0; i < m ;i++)
{
if (input & MASK){
strcpy(array[i],"1");}
else{
strcpy(array[i],"0");}
input = input << 1;
}
return (array);
}

int power (int base, int m)
{
static int i;
int p=1;
for (i = 1; i <= m; ++i)
{
p =p * base;
}
return p;
}
Aug 11 '08
12 1641
Martien Verbruggen said:
On Mon, 11 Aug 2008 12:02:39 +0000,
Richard Heathfield <rj*@see.sig.in validwrote:
>>
char *dec2bin(int n)

Following to your own explanation, above, shouldn't this be called
int2bin(), or maybe num2bin()?
int2bin would be favourite, I suppose. But of course I was lazy enough to
retain the OP's function name rather than take the trouble to think about
it.

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 12 '08 #11
Ben Bacarisse said:
Richard Heathfield <rj*@see.sig.in validwrites:
>Nezhate said:
>>I'm writing a program that takes a decimal number
<snip>
>>and returns it's binary equivalent.

Here's one way:

#include <limits.h>
#include <stddef.h>

char *dec2bin(int n)
{
static char bin[sizeof n * CHAR_BIT + 1];
size_t b = sizeof n * CHAR_BIT;
size_t i = 0;

while(b-- 0)
{
bin[i++] = '0' + !!(n & (1 << b));

Warning: this is UB in C99 (a signed shift into the sign bit
position[1]).
In 6.5.7(4), we have: "The result of E1 << E2 is E1 left-shifted E2 bit
positions; vacated bits are filled with zeros. If E1 has an unsigned type,
the value of the result is E1 × 2E2, reduced modulo one more than the
maximum value representable in the result type. If E1 has a signed type
and nonnegative value, and E1 × 2E2 is representable in the result type,
then that is the resulting value; otherwise, the behavior is undefined."

So yes, you're right, as a result of which I had another stab at this, but
fell foul of right-shifting an int (where the sign bit propagation is a
problem), and I thought about a couple of other approaches, but then I
decided it would be a lot easier just to drink my coffee and wake up
slowly. :-)

--
Richard Heathfield <http://www.cpax.org.uk >
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 12 '08 #12
Richard Heathfield wrote:
Iman S. H. Suyoto said:
>Richard Heathfield wrote:
>>Here's one way:

#include <limits.h>
#include <stddef.h>

char *dec2bin(int n)
{
static char bin[sizeof n * CHAR_BIT + 1];
CMIIW. If the + 1 is for the '\0' terminator, don't we need that
terminator there?

Yes, and it's there. From 3.5.7 (Initialization ):

"If an object that has static storage duration is not initialized
explicitly, it is initialized implicitly as if every member that has
arithmetic type were assigned 0 and every member that has pointer type
were assigned a null pointer constant. If an object that has
automatic storage duration is not initialized explicitly, its value is
indeterminate."
Right. Sorry, I forgot about that.

Thanks, Richard.
Aug 13 '08 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1949
by: Tommy Lang | last post by:
I am working on this project and I need some help/pointers/comments to get me started, I am stuck. The program will be used to store information in an array while it is running. I need to store objects of my classes Person(superclass), Student(inherit Person), Teacher(inherit Person) in that array. The name will be the unique key. These classes are all working ok. I want to be able to add, remove, find etc. objects. To all of this, I...
9
2846
by: Simon | last post by:
Hi, if I have a structure like... struct sMyPointers{ ClassA *m_pPointerA; ClassB *m_pPointerB; void *m_pPointerToSomethingElse; };
0
2611
by: rokuingh | last post by:
ok, so i've been working on this one for quite a while, and the code is very big so i'm just going to give the relevant parts. this is a program that builds polymers (chemical structures of repeated monomers) which are represented as doubly pointed noncomplete binary trees. There are three different types of monomers (hence the three different constructer calling functions) the first one is the "leaves" of the tree, the second adds length...
1
1788
by: ArcInversion | last post by:
Hi, I've been using a javascript script to create a dragon that flies across the page. Anyways, I'd like to make it so when you click the dragon it takes you to a new page. Was wondering if anyone could help me out here. Below is the complete script. <SCRIPT language="JavaScript1.2"> var cursorpath="http://i68.photobucket.com/albums/i9/worklog_halcyon/Misc/pet1_lohi_dog.png" if (document.layers)
23
2561
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
11
1534
by: sarathy | last post by:
Hi, I have been using C++ for a while. I am not entirely clear with the concepts of reference in C++. - Why was there a need for introducing a concept called "Reference" in C++ when everything was working fine with normal pointers? - Is there anything that a reference can do that a pointer cannot ??? Please clarify,
5
3377
by: Y2J | last post by:
I am working through this book on C++ programming, the author is speaking of using linked lists. He gave and example which I found confusing to say the least. So I rewrote the example in a way that I could better understand the concept, he was trying to convey to me. I ran my own example and it crashed and burn "what a surprise!" : (. I ran the authors example out of the book and quess what, it crashed also, : 0. I ran them both on my...
20
4657
by: Martin Jørgensen | last post by:
Hi, I'm reading a number of double values from a file. It's a 2D-array: 1 2 3 4 5 6 7 ------------- 1 3.2 2 0 2.1 3 9.3 4
7
1403
by: DDP3000 | last post by:
void F(int **A, int N) { int i,j; for(i=0;i<N;i++) for(j=0;j<N;j++) A=((i+j)%2==0)?1:-1; } I have never used such a thing before, so it might be a really stupid question but I cannot find the answer.
0
9714
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10346
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10347
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10090
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9173
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.