-1

probably there is a smart way to do that , but anyway i get error on this :

-(int*)decimalBinary:(int)decimal
{
    int i=0;
    int *bin;
    while (decimal!=0)
    {
        bin[i]=decimal%2;
        decimal=decimal/2;
        i++;
    }

    return bin;

}

on the modulo line . why ? And whats the better way to get it to array ?

Curnelious
  • 1
  • 16
  • 76
  • 150
  • Print binary batter use bitwise operators: read: [Decimal to Binary: Size independent](http://stackoverflow.com/questions/14104208/convert-integer-to-binary-and-store-it-in-an-integer-array-of-specified-sizec/14314522#14314522) – Grijesh Chauhan Sep 18 '13 at 16:45
  • 5
    I'm not familiar with objective c, but shouldn't a pointer be initialised before use? In plain C, you'd have to initialise the pointer with either passing an array to it, or (m|c|?)alloc() functions... – ppeterka Sep 18 '13 at 16:46
  • Change `int *bin` to `int bin[sizeof(int) + 1`. Set a point to end of `buf` and fill backwards, decrementing the pointer each step. – chux - Reinstate Monica Sep 18 '13 at 16:49
  • i dont think it has to do with objC, its just a C pointer . but i did tried to initialize it and still got error. if i set :b[1] instead of zero , its not crashing . – Curnelious Sep 18 '13 at 16:49
  • Look this answer http://stackoverflow.com/questions/11797460/convert-decimal-to-binary-bitmap-of-a-character – jdiego Sep 18 '13 at 16:50
  • Thanks but i dont get your answer. i used to set pointers like that many times ,bin is pointing on an address to start the array,and does not need a size . whats happen now ? – Curnelious Sep 18 '13 at 16:50
  • What compiler message do you get? Could it be *"variable 'bin' is uninitialized when used here"*? – Martin R Sep 18 '13 at 16:52
  • No. i get crash-no message . – Curnelious Sep 18 '13 at 16:55
  • Got it . i just will not use pointer ,instead bin[4], and return int and not pointer. – Curnelious Sep 18 '13 at 16:57
  • @Curnelious; No, this will not work (in C). You can't return a pointer to automatic local variable. `bin[4]` is local variable. – haccks Sep 18 '13 at 17:03
  • So, how DO YOU CREATE a 4 different numbers ? how do you handle that C arrays ? i need 4 separate bits that i can check individually later. – Curnelious Sep 18 '13 at 17:13
  • @Curnelious - I think ***[this](http://stackoverflow.com/a/112956/645128)*** will do what you are looking for. There is an adaptation of it in my answer below. Good question, it is obviously interesting to many people. – ryyker Sep 19 '13 at 16:11

2 Answers2

0

Declaring

int *bin;

sets aside space for a pointer but doesn't make it point to an object. It is crucial to initialize bin before using it.
To solve your problem you can declare an array bin[4] in caller function (int main) and then pass *bin to your calling function.

haccks
  • 104,019
  • 25
  • 176
  • 264
0

The following code is adapted from This answer on how to print an integer in binary format. Storing "binary digits" into an int array is added into the code below:

#include <stdio.h>      /* printf */
#include <stdlib.h>     /* strtol */

const char *byte_to_binary(long x);

int main(void)
{
    long lVal;
    int i, len, array[18];
    char buf[18];

    {   /* binary string to int */
        char *tmp;
        char *b = "11010111001010110";

        lVal=strtol(b, &tmp, 2); //convert string in "base 2" format to long int
        printf("%d\n", lVal);
    }
    {
        printf("%s", byte_to_binary(lVal));
        /* byte to binary string */
        sprintf(buf,"%s", byte_to_binary(lVal));
    }
    len = strlen(buf);
    for(i=0;i<len;i++)
    {   //store binary digits into an array.
        array[i] = (buf[i]-'0');    
    }
    getchar();
    return 0;
}

const char *byte_to_binary(long x)
{
    static char b[17]; //16 bits plus '\0'
    b[0] = '\0';
    char *p = b;  

    int z;
    for (z = 65536; z > 0; z >>= 1)    //2^16
    {
        *p++ = (x & z) ? '1' : '0';
    }
    return b;
}
Community
  • 1
  • 1
ryyker
  • 22,849
  • 3
  • 43
  • 87