Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Gettng rid of local address,

Status
Not open for further replies.

countdrak

Programmer
Jun 20, 2003
358
US

Code:
char* dec2bin(int decimal);

int main()
{
	int x = 0;
	char* ans;
	int i;
	for (i=0; i<10; i++)
	{
		ans = dec2bin(x);
		printf("x = %d ===> %s\n", x, ans);
		x++;
	}

	return 0;
}


char* dec2bin(int decimal)
{
        int  k = 0, n = 0;
        int  neg_flag = 0;
        int  remain;
        char temp[80];
		char binary[80];

        // take care of negative input
        if (decimal < 0)
        {
                decimal = -decimal;
                neg_flag = 1;
        }
        do
        {
                remain    = decimal % 2;
                decimal   = decimal / 2;   // whittle down decimal
                temp[k++] = remain + 0x30; // makes characters 0 or 1
        } while (decimal > 0);

        if (neg_flag)
        temp[k++] = '-';           // add back - sign
        else
        temp[k++] = ' ';           // or space
        while (k >= 0)
        {
                binary[n++] = temp[--k];   // reverse spelling
        }
        binary[n-1] = 0;             // end with NULL
		return binary;
}

This is exactly what I need...in essence. But I am returning address of a local variable, how to get around it? I need it to return a value, not pass a variable in.

Also, for some reason, my 1st value doesn't get printed. I am guessing its coz of the local variable problem, but I could be wrond. Meh?!?!

Thhanks a lot guys.
 
Well, the variable [tt]binary[/tt] is only going to exist on the stack while in the function [tt]dec2bin[/tt]. The way you have it defined, you won't ever be able to get the value of the variable.

You should define [tt]binary[/tt] in [tt]main()[/tt] and pass the address of it to [tt]dec2bin[/tt].
Code:
int main()
{
...
char ans[80];
...
dec2bin(x, &ans);
...
}

void dec2bin(int decimal, char * binary)
{
...
Something like this should work. Maybe change to a return code you can test for success or failure.

Or, you could have it [tt]malloc[/tt] memory for it, but then you'd need to make sure you [tt]free[/tt] it when done.
 
Ya, I want the function to return the value in a X = my_func(Y) fashion.

*sigh* I guess malloc is my only option. Thanks a lot.
 
You could make binary[] static in the function dec2bin().

Then it would still exist when you returned from the function.

In fact it would exist for the lifetime of the program.
 
> You could make binary[] static in the function dec2bin().
You could, until you did something like
[tt]printf("%s %s\n", dec2bin(1), dec2bin(2) );[/tt]

> *sigh* I guess malloc is my only option. Thanks a lot.
Which becomes an additional burden on the caller, and a memory leak if you try and do things like
[tt]printf("%s %s\n", dec2bin(1), dec2bin(2) );[/tt]
(ohhh, deva-vue)

Passing the pointer to where you want the answer stored is generally the most flexible approach for the caller, and leaves the called function with nothing to worry about except working out the answer.

--
 
Code:
#include<stdio.h>

char* dec2bin(int decimal, char * buff );

int main()
{
	 char buff1[ 80 ], buff2[ 80 ];

	 int x = 0;
	 char* ans;
	 int i;
	 for (i=0; i<10; i++)
	 {
		  //ans = dec2bin(x);
		  //printf("x = %d ===> %s\n", x, ans);
		  printf( "x = %d ==> %s %s\n", x, dec2bin(++x, buff2), dec2bin(x, buff1 ));
		  x++;
	 }

	 return 0;
}


char* dec2bin(int decimal, char *binary )
{
		  int  k = 0, n = 0;
		  int  neg_flag = 0;
		  int  remain;
		  char temp[80];
//		  /*static*/ char binary[80];

		  // take care of negative input
		  if (decimal < 0)
		  {
					 decimal = -decimal;
					 neg_flag = 1;
		  }
		  do
		  {
					 remain    = decimal % 2;
					 decimal   = decimal / 2;   // whittle down decimal
					 temp[k++] = remain + 0x30; // makes characters 0 or 1
		  } while (decimal > 0);

		  if (neg_flag)
        temp[k++] = '-';           // add back - sign
        else
        temp[k++] = ' ';           // or space
        while (k >= 0)
        {
                binary[n++] = temp[--k];   // reverse spelling
        }
        binary[n-1] = 0;             // end with NULL
        return binary;
}

sort of works...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top