Scape
Scape

Reputation: 59

C store multiple int arrays into another array via pointer

I'm sorry if this is confusing....So far I'm converting a decimal number into binary. While doing this, i store the digits for the binary representation into an int array.

EX: for the number 4. (this is done in dec2bin below)

    temp[0] = 1
    temp[1] = 0
    temp[2] = 0

i would like to store this array into another array (say BinaryArray) that will contain multiple 'temp' arrays.

I would like the BinaryArray to declared main, passed to dec2bin, and the save a copy of the current temp array. then go to the next number.

I'm having trouble with figuring out the pointers and what not needed for this. If someone could help me with how to declare the needed array in main and how add to it from dec2bin.

Thanks! Main:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    int main()
    {      

      void dec2bin(int term, int size);

      int size, mincount;
      int * ptr;
      int x;
      x=0;

      scanf("%d %d", &size, &mincount);
      printf("Variables: %d\n", size);
      printf("Count of minterms: %d\n", mincount);

      int input[mincount+1];

      while(x < mincount){
        scanf("%d", &input[x]);
        x++;
      }
      x = 0;

      while(x < mincount){
        dec2bin(input[x], size);

Dec2bin :

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define SIZE 32

    void
    dec2bin(int term,int size){
      int i, j, temp[size], remain, quotient;
      quotient = term;
      i = size-1;
      // set all temp to 0
      for(j=size-1;j>=0; j--){
        temp[j] = 0;
        }

      //change to binary
      while(quotient != 0){
        remain = quotient % 2;
        quotient/=2;
        if(remain != 0){
          temp[i] = 1;
         } else {
          temp[i] = 0;
         }
         i--;
        }

        //print array
        for(i=0; i<size; i++)
          printf("%d", temp[i]);

        printf("\n");
    }

Upvotes: 1

Views: 10769

Answers (2)

Pedro Alves
Pedro Alves

Reputation: 1728

Don't have sure if i understood what you want to do, but it seems that you want to create a "array of arrays of int". Example:

#include <stdio.h>
#include <stdlib.h>

int main(){
    int i;
    int n;
    int **myArray;

    n = 10;
    myArray = (int**)malloc(n*sizeof(int*));

    //Usage example
    int myIntArray[] = {1,2,3,4,5};

    myArray[0] = myIntArray;

    //This call should print "4"
    printf("%d\n",myArray[0][3]);

    return;
}   

This way you will have a array (myArray) that each element is a array of ints.

Upvotes: 6

bspikol
bspikol

Reputation: 96

To "set all temp to 0", use memset(). I assume you want to display an integer in binary. You can check each bit by performing a logical and with 0x80000000 and then left shifting the variable. Here is a crude example:

int x = 27;
string bin;

for ( int index = 0; index < sizeof(int) * 8; ++index ) {
if ( x & 0x80000000 ) {
    bin += '1';
} else {
    bin += '0';
}
x = x << 1;
}
cout << bin << endl;

Why do you want to store a binary representation of an integer in an array of ints? I can't think of a reason to do this.

Upvotes: 0

Related Questions