-1

I have int *b and a function

void to_Binary(int num, int range, int **bi_res)
{
   int k = num, c = 0, r;
   *bi_res = (int *) calloc(range,sizeof(int));

   while (range >= c) {
        r = k%2; 
        c++; 
        (*bi_res)[range-c] = r; 
        k /= 2;
    }

   return;
}

This is an example of how I send b to the function

 void main
{
   // A small example if you want to replicate. 
   int *b;
   int i = 0;
   to_Binary(56,6,&b);
   while (i < 6) {printf("%d\n",b[i]); i++;}
   return;
} 

but it gives me seg fault in the loop

Note: The function itself works fine and does what it has to do if I access it in other ways but in the case I am required I have to return an array of ints that I can't know the size of

12
  • Because of operator precedence, your code is equivalent to *(bi_res[range-c]) = r;. You should get a warning during compilation about this. Commented Feb 24, 2021 at 17:50
  • @xing Doesn't work Commented Feb 24, 2021 at 17:50
  • @Bobo I know that I need to pass b as &b but it doesn't work Commented Feb 24, 2021 at 17:51
  • Is there a reason why you use a while loop instead of for loop? Commented Feb 24, 2021 at 17:53
  • 1
    The loop condition would be easier to understand if you wrote for (index = range-1; index >= 0; index--) and then used [index]. Commented Feb 24, 2021 at 18:01

1 Answer 1

2

There are at least two problems with the while loop. The first one is that you are using an incorrect expression in this statement

*bi_res[range-c] = r;

You have to write

( *bi_res )[range-c] = r;

The second one is that if you will even use the correct expression nevertheless when range is equal to c you will try to access beyond the allocated array due to this increment within the loop

c++;

Thus the expression

range-c

will have a negative value.

You could write the loop like

while (range != c) {
    //...
}

And this statement

k =/ 2;

contains a syntax error. You mean

k /= 2;

Pay attention to that the return statement is redundant.

And the function main should be declared like

int main( void )

and you should free the allocated memory in main.

Sign up to request clarification or add additional context in comments.

5 Comments

Explain how to fix the use of c. I think they just need to change >= to >, right?
As I told @Barmar I tried to use (*bi_res)[range-c] but it doesn't work and yeah my bad I didn't copy paste and did in my program /= not =/ the function works as it should if I send an array that is ready but as I wrote below I need to allocate memory to the array inside the function
did you fix the loop condition?
=/ was a copying error, not in the actual code, you can remove it from the answer.
Your memory allocation is fine, the problems are with other parts of the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.