Member Avatar for schwarznavy

Hi. I'm trying to call a function to convert a decimal integer into a binary number (stored as an array of integers). Before I knew that I couldn't return an array, this is how I thought to implement it. Can anyone help me understand the way I'm actually supposed to do this?
Thank you very much.
Here is MAIN:

#include <stdio.h>
#include <math.h>
int dec2bin (int decimal);
    
int main(void) {
    int binary[8]; // creates array to store 8-bit binary number
    int decimal=5; // with a test value. final version will have user input.

    binary = dec2bin(decimal);

And my function:

int dec2bin (int decimal) {
    int binary[8];
    int x;
       
    for (x=8; x>0; x--) { 
        binary[x] = decimal % 2;
        decimal /= 2;
        printf("%d", binary[x]);
    }
    return binary;
}

Recommended Answers

All 10 Replies

Member Avatar for schwarznavy

I forgot to mention that what I'll be passing will be a struct array.
Something like user_octet[0].input and I'll be expecting user_octet[0].input_binary back.

Not sure if that makes a difference.

Hi. I'm trying to call a function to convert a decimal integer into a binary number (stored as an array of integers). Before I knew that I couldn't return an array, this is how I thought to implement it. Can anyone help me understand the way I'm actually supposed to do this?
Thank you very much.
Here is MAIN:

#include <stdio.h>
#include <math.h>
int dec2bin (int decimal);
    
int main(void) {
    int binary[8]; // creates array to store 8-bit binary number
    int decimal=5; // with a test value. final version will have user input.

    binary = dec2bin(decimal);

And my function:

int dec2bin (int decimal) {
    int binary[8];
    int x;
       
    for (x=8; x>0; x--) { 
        binary[x] = decimal % 2;
        decimal /= 2;
        printf("%d", binary[x]);
    }
    return binary;
}

Your returning an array that's created on the stack...This is never a good idea because when the function returns it releases its stack memory....therefore invalidating it(the array binary[8]).

Member Avatar for schwarznavy

Thank you for your reply. I didn't know what the problem was, thank you for explaining. I DO know that it doesn't work.
Do you have any suggestions on what to change?

Thank you for your reply. I didn't know what the problem was, thank you for explaining. I DO know that it doesn't work.
Do you have any suggestions on what to change?

You could pass the array to the function as a parameter.

Member Avatar for schwarznavy

Thank you again for the quick reply. I'm pretty new to programming. Could you by any chance show me an example of what you mean?

In essence, I'm going to pass user_octet[0].input and I'll be expecting the 8-element array user_octet[0].input_binary[x] back. Mose of the examples I've seen on the web are shown with passing an array to the function. In my case, I'm only passing one element of an array TO the function, but I'm expecting an entire 8-element back. (I'm trying to convert a decimal integer to an 8-bit binary.)
Thank you.

To pass the array try something like below..

#include <stdio.h>

#define ARR_SIZE 8

void myfunc(int *myint)
{
	unsigned int i = 0;

	for (i = 0; i < ARR_SIZE; ++i)
		myint[i] = i + 5; 
}

int main(int argc, char**argv)
{
	unsigned int i = 0;
	int array[ARR_SIZE];

	myfunc(&array[0]);

	for (i = 0; i < ARR_SIZE; ++i)
		fprintf(stdout, "array[%u]->%d\n", i, array[i]);
	return 0;
}
Member Avatar for schwarznavy

To pass the array try something like below..

#include <stdio.h>

#define ARR_SIZE 8

void myfunc(int *myint)
{
	unsigned int i = 0;

	for (i = 0; i < ARR_SIZE; ++i)
		myint[i] = i + 5; 
}

int main(int argc, char**argv)
{
	unsigned int i = 0;
	int array[ARR_SIZE];

	myfunc(&array[0]);

	for (i = 0; i < ARR_SIZE; ++i)
		fprintf(stdout, "array[%u]->%d\n", i, array[i]);
	return 0;
}

Gerard -- that is assuming the varible being received and passed are the same, right? In my case, that isn't the case. I'm passing one element of a structured array ([x].input), but I'm receiving a completely different array ([x].input_binary[]).

Gerard -- that is assuming the varible being received and passed are the same, right? In my case, that isn't the case. I'm passing one element of a structured array ([x].input), but I'm receiving a completely different array ([x].input_binary[]).

This was an example of passing the array. If you want to pass both, then create a function with two parameters.

myfunc(&array[0]);

This complexity is not necessary. myfunc(array); is enough.

Member Avatar for schwarznavy
myfunc(&array[0]);

This complexity is not necessary. myfunc(array); is enough.

I appreciate the support. Not sure how this comment above helps, though.

I figured out a solution that works for me. It goes like this:

void dec2bin(int decimalnum, int binarynum[], int x);

int main(void)
{   int input1;
     int input1.binary[8];
     int i;

     dec2bin(#//some number//, input1_binary, 8);
void dec2bin(int decimalnum, int binarynum[], int x)
{
     for (; x>0; x--)
     {  binarynum[x-1] = decimalnum % 2;
         decimalnum /= 2;
     }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.