[英]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.我收到错误“二进制 + 的操作数无效(有 int* 和 int*)”,我不知道为什么它向我抛出这个错误。 how would I go about adding the array elements of the 2 arrays.我将如何添加 2 个数组的数组元素。 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.然后我像这样在 main 中调用函数。
int* add1 = complement_2_add(complement2Array1, complement2Array2, j);
Complement2Array1 and Complement2Array2 are defined the same. Complement2Array1 和 Complement2Array2 的定义相同。
int* complement2Array1 = signed2complement2(signedIntArray1, j);
and signed2complement2 is defined as.并且signed2complement2被定义为。
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 .在这个first_complement2[count]
, second_compelment[count]
都是整数指针。
As both first_complement
and second_complement
are array of integer pointers.因为first_complement
和second_complement
都是整数指针数组。
You need to dereference them before adding -您需要在添加之前取消引用它们 -
remainder = (*first_complement2[count]) + (*second_compelment[count]) + carryover;
Didn't you got any warning or error for passing a int *
to a function which expects array of integer pointer or int **
?将int *
传递给需要整数指针或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
. complement2Array1
和complement2array2
必须是int **
类型或者是int pointers
数组。
Or simple as @alk Sir suggested use int *
instead of int **
.或者像@alk Sir 建议的那样简单,使用int *
而不是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.从您使用first_complement2
和second_compelment
的方式以及您调用complement2_add()
second_compelment
complement2_add()
的方式second_compelment
,这是错误的。
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;
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.