我的C问题是如何正确地获取我在binaryConversion()中获得的二进制数组并将其复制到makeInt()中的integer.binary中。
我试图使binaryConversion()返回一个指针,然后将该指针取消引用并复制到新数组中,但是我几乎可以肯定这在某种程度上是错误的。 这是我失败的尝试:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
struct Integer{
int decimal;
int binary[32];
};
int * binaryConversion(int num){
int bin_buffer[32];
int * ptr = bin_buffer;
unsigned int mask = INT_MIN;
for(int i = 0; i < 32; i++){
if(mask & num == 1){
bin_buffer[i] = 1;
}
else{
bin_buffer[i] = 0;
}
mask >>= 1;
}
return ptr;
}
struct Integer makeInt(int num){
struct Integer integer;
int * ptr = binaryConversion(num);
for(int i = 0; i < 32; i++){
integer.binary[i] = *(ptr+i);
}
}
,它必须与此驱动程序代码一起使用:
struct Integer makeInt(int);
void binaryTester(int array[], int test[], int size){
int i;
for(i = 0; i < size; i++){
assert(array[i] == test[i]);
}
printf("\n");
}
int main(){
int size = sizeof(int) * 8;
int array[size], i;
struct Integer test;
printf("\n\t=========Test #7: Conversion of 2===========\n\n");
test = makeInt(2);
int test1[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0};
printf("\n\t\t....Converting 2 Passed\n");
}
谢谢