Sca*_*ape 1 c arrays pointers multidimensional-array
如果这令人困惑,我很抱歉....到目前为止,我正在将十进制数转换为二进制数.在执行此操作时,我将二进制表示的数字存储到int数组中.
EX:对于数字4.(这在下面的dec2bin中完成)
temp[0] = 1
temp[1] = 0
temp[2] = 0
Run Code Online (Sandbox Code Playgroud)
我想将这个数组存储到另一个包含多个'temp'数组的数组(比如BinaryArray)中.
我希望BinaryArray声明为main,传递给dec2bin,并保存当前临时数组的副本.然后转到下一个号码.
我在找出指针以及不需要的东西时遇到了麻烦.如果有人可以帮助我如何在main中声明所需的数组以及如何从dec2bin添加它.
谢谢!主要:
#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);
Run Code Online (Sandbox Code Playgroud)
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");
}
Run Code Online (Sandbox Code Playgroud)
不确定我是否理解你想做什么,但似乎你想创建一个"int数组数组".例:
#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;
}
Run Code Online (Sandbox Code Playgroud)
这样你就有了一个数组(myArray),每个元素都是一个int数组.