简体   繁体   中英

Adding binary numbers in C

Im getting the error "invalid operands to binary + (have int* and int*)" and i don't know why It is throwing me this error. how would I go about adding the array elements of the 2 arrays. note both arrays are the same size.

int* complement2_add(int* first_complement2[], int* second_compelment[], int size){
int count = 0,remainder = 0, carryover = 0;

int* cAdd = (int*)malloc(size*sizeof(int));


for(count = size-1; count >= 0; count--){
    remainder = first_complement2[count] + second_compelment[count] + carryover;

    if(remainder == 1){
        cAdd[count] = 1;
        carryover = 0;
    }
    else if(remainder == 2){
        cAdd[count] = 0;
        carryover = 1;
    }
    else if(remainder == 3){
        cAdd[count] = 1;
        carryover = 1;
    }
    else if(remainder == 0){
        cAdd[count] =0;
        carryover = 0;
    }

}
if(carryover == 1){
    cAdd[count] = 1;
}
return cAdd;

}

Then I am calling the function in main like this.

    int* add1 = complement_2_add(complement2Array1, complement2Array2, j);

Complement2Array1 and Complement2Array2 are defined the same.

int* complement2Array1 = signed2complement2(signedIntArray1, j);

and signed2complement2 is defined as.

int* signed2complement2(int signed_binary[], int size){
  int i = 0;
  int* complemt2Array = (int*)malloc(size*sizeof(int));
  int flipflag = 0;
  for(i = size-1; i>=0;i-- ){
    if(flipflag == 0){
        complemt2Array[i] = signed_binary[i];
        if(signed_binary[i] == 1){
            flipflag = 1;
        }
    }
    else{
        complemt2Array[i] = signed_binary[i] == 0?1:0;
    }
  }
  return complemt2Array;
}
  remainder = first_complement2[count] + second_compelment[count] + carryover;

In this first_complement2[count] , second_compelment[count] both are integer pointers .

As both first_complement and second_complement are array of integer pointers.

You need to dereference them before adding -

remainder = (*first_complement2[count]) + (*second_compelment[count]) + carryover;

EDIT

Didn't you got any warning or error for passing a int * to a function which expects array of integer pointer or int ** ?

int* complement2Array1 = signed2complement2(signedIntArray1, j);    // it is an int *

complement2Array1 and complement2array2 has to be of type int ** or to be array of int pointers .

Or simple as @alk Sir suggested use int * instead of int ** .

This

int* complement2_add(int* first_complement2[], int* second_compelment[], int size){

if equal to

int* complement2_add(int ** first_complement2, int ** second_compelment, int size){

which from the way you use first_complement2 and second_compelment and the way you call complement2_add() is wrong.

It seems you wanted

int* complement2_add(int * first_complement2, int * second_compelment, int size){

Also add the prototype to it before it is used:

int* complement2_add(int * first_complement2, int * second_compelment, int size);

... somefunc(...)
{
  ...

   int* add1 = complement_2_add(complement2Array1, complement2Array2, j);

   ...
}

int* complement2_add(int* first_complement2, int* second_compelment, int size)
{
  int count = 0,remainder = 0, carryover = 0;
  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM