473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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,dec2bin(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 #1
12 1607
Nezhate wrote:
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,dec2bin(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;
}
This is the simplest fix
which only addresses the one issue raised by your compiler:

#include <limits.h>

char* dec2bin(int input, int m)
{
static int i = 0;
static char array[sizeof input * CHAR_BIT + 1];
int 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){
array[i] = '1';
} else {
array[i] = '0';
}
input = input << 1;
}
return (array);
}

--
pete
Aug 11 '08 #2
Nezhate said:
Hi There,
I'm writing a program that takes a decimal number
No such thing. Numbers are numbers. Decimal is a positional notation
system. Numbers can be represented in lots of different ways.
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));
}

return bin;
}

--
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 11 '08 #3
Richard Heathfield wrote:
Nezhate said:
>Hi There,
I'm writing a program that takes a decimal number

No such thing. Numbers are numbers. Decimal is a positional notation
system. Numbers can be represented in lots of different ways.
>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];
CMIIW. If the + 1 is for the '\0' terminator, don't we need that
terminator there?
size_t b = sizeof n * CHAR_BIT;
size_t i = 0;

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

return bin;
}
Aug 11 '08 #4
Iman S. H. Suyoto said:
Richard Heathfield wrote:
>Nezhate said:
>>Hi There,
I'm writing a program that takes a decimal number

No such thing. Numbers are numbers. Decimal is a positional notation
system. Numbers can be represented in lots of different ways.
>>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];

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

Since the object in question has static storage duration, every member
thereof (less any that are explicitly initialised, which doesn't apply
here) is given the value 0. Note that the above passage also makes it
clear that the same guarantee does *not* apply to objects with automatic
storage duration. (It *would* apply, if the automatic object were partly
initialised - the Standard says this a few paragraphs further on.)

--
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 11 '08 #5
On Aug 11, 6:07*am, Nezhate <ma************@gmail.comwrote:
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,dec2bin(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;

}

This are the alternatives for returning a string from a function:

- Declare the array to be static, as Pete and Richard Heathfield
did
- Use a dynamic memory allocation function to allocate the array,
but remember to free() the string when you no longer need it
- Declare the function to take a pointer to an array as an
argument, and perhaps an integer representing the maximum number of
characters the buffer can hold
- Make the function operate on a global variable (yuck, don't do
this)

I'd go with the second, since it doesn't retain unneeded memory.

Sebastian

Aug 11 '08 #6
s0****@gmail.com wrote:
This are the alternatives for returning a string from a function:

- Declare the array to be static, as Pete and Richard Heathfield
did
- Use a dynamic memory allocation function to allocate the array,
but remember to free() the string when you no longer need it
- Declare the function to take a pointer to an array as an
argument, and perhaps an integer representing the maximum number of
characters the buffer can hold
- Make the function operate on a global variable (yuck, don't do
this)

I'd go with the second, since it doesn't retain unneeded memory.
The third would be my default choice.
That way an array with any kind of duration can be used,
and if it's allocated, then the allocation and freeing
can both be done in the calling function,
which is nice from an organizational point of view.

--
pete
Aug 11 '08 #7
On Mon, 11 Aug 2008 12:02:39 +0000,
Richard Heathfield <rj*@see.sig.invalidwrote:
Nezhate said:
>Hi There,
I'm writing a program that takes a decimal number

No such thing. Numbers are numbers. Decimal is a positional notation
system. Numbers can be represented in lots of different ways.
>and returns it's binary equivalent.

Here's one way:

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

char *dec2bin(int n)
Following to your own explanation, above, shouldn't this be called
int2bin(), or maybe num2bin()?

Something called dec2bin probably should have a prototype like:

char *dec2bin(char *dec);

Martien
--
|
Martien Verbruggen | I used to have a Heisenbergmobile. Every time
| I looked at the speedometer, I got lost.
|
Aug 11 '08 #8
Richard Heathfield <rj*@see.sig.invalidwrites:
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]). I presume it is OK in C90, but the wording seems vague
to me ("The result of E1 << E2 is E1 left-shifted E2 bit positions" is
all it says about shifts of signed types).
}

return bin;
}
[1] I am paraphrasing, of course (the result is defined only if 1
times 2 to the power b is representable as an int).

--
Ben.
Aug 12 '08 #9
On Mon, 11 Aug 2008 04:07:24 -0700 (PDT), Nezhate
<ma************@gmail.comwrote:
>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,dec2bin(input, m));
exit (0);
}

char* dec2bin(int input, int m)
{
static int i = 0;
char* array[m+1];
This defines an array of pointers, not an array of char which you
need.

You have a C99 compiler which allows you to use variable length
arrays?
int limit = 0;
limit = power(2, m);
const int MASK = limit/2; // the binary equivalent is: 1 + m 0 bits
Not after you divide by 2. The binary equivalent has only m-1 zero
bits after the one.
>
array[m] = '\0';
This sets array[m] to NULL.
for (i= 0; i < m ;i++)
{
if (input & MASK){
strcpy(array[i],"1");}
This invokes undefined behavior. array[i] does not point to memory
you own. It was never assigned a value so it is indeterminate.
else{
strcpy(array[i],"0");}
input = input << 1;
Using this approach with a large input and 32 bit int will invoke
undefined behavior when a 1 bit is shifted into the sign bit.
}
return (array);
return is a statement, not a function. The parentheses are
unnecessary.

The expression array has type array of pointer to char. By rule, it
is converted to the address of the first element of the array with
type pointer to array element (essentially &array[0]). In this case,
element type is char* so the expression is converted to type char**.
char** is incompatible with the specified return type (char*) of the
function. And since array is an automatic variable in the function,
it ceases to exist once the function exits so any attempt by the
calling program to evaluate this address invokes undefined behavior.
>}

int power (int base, int m)
{
static int i;
What do you think the static buys you?
int p=1;
for (i = 1; i <= m; ++i)
{
p =p * base;
}
return p;
}
--
Remove del for email
Aug 12 '08 #10
Martien Verbruggen said:
On Mon, 11 Aug 2008 12:02:39 +0000,
Richard Heathfield <rj*@see.sig.invalidwrote:
>>
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.invalidwrites:
>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
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...
9
by: Simon | last post by:
Hi, if I have a structure like... struct sMyPointers{ ClassA *m_pPointerA; ClassB *m_pPointerB; void *m_pPointerToSomethingElse; };
0
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...
1
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...
23
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
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...
5
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...
20
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.