Thread: Code not reuturning array values

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    1

    Code not reuturning array values

    // function to convert decimal to binary
    Code:
    int *decToBinary(int n) 
    { 
        // array to store binary number 
        static int binNum[500]; //, array_bin[31]; 
        
        //printf("number as input%d\n",n);
        int dec_num = n;
      
        // counter for binary array 
        int i = 0 ,k ,j ; 
        if (n == 0)
        {
         binNum[i] = 0; 
            //n = n / 2; 
            i++; 
        }
        while (n != 0) { 
            // storing remainder in binary array 
            binNum[i] = n % 2; 
                n = n / 2; 
               i++; 
        } 
         //int j;
        // printing binary array in reverse order 
        for (int j = i - 1 ; j >= 0 ; j--)
            //if (i>0)
            //{printf ("dectobin for loop%d",j);
           //printf("The binary equivalent of %d is %d. %d %d\n", dec_num, binNum[j],j,i);
           //i--; }
           return binNum;
        }
    need help

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    At a quick glance it looks like you've removed too much from that last for() loop. That loop at most runs once, at worst it will cause the function to fail to return a value (the return is skipped).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-21-2013, 10:35 PM
  2. why can't a static array copy values from a dynamic array
    By c++noob145 in forum C++ Programming
    Replies: 2
    Last Post: 03-13-2013, 10:25 AM
  3. Replies: 26
    Last Post: 06-04-2012, 07:58 PM
  4. Finding an array of unique values within a 2D array
    By saadashfaq in forum C Programming
    Replies: 6
    Last Post: 11-14-2011, 12:02 AM
  5. sizeof(array) returns differnt values for same array?
    By Diamonds in forum C++ Programming
    Replies: 6
    Last Post: 02-02-2003, 04:27 PM

Tags for this Thread