用c语言重新填充二维数组
我有两个相同大小的2d数组“a”,“b”(空数组),我必须更改“a”,因为某个函数将其新值保存在“b”中,然后我必须根据相同的函数更改新值,因此程序将b的新值保存在a中,然后返回a。 打印阵列时,仅打印前两个阵列用c语言重新填充二维数组,c,arrays,pointers,C,Arrays,Pointers,我有两个相同大小的2d数组“a”,“b”(空数组),我必须更改“a”,因为某个函数将其新值保存在“b”中,然后我必须根据相同的函数更改新值,因此程序将b的新值保存在a中,然后返回a。 打印阵列时,仅打印前两个阵列 #include<stdio.h> #include<stdlib.h> #include<string.h> #define MSIZE 10 void new_gen(char * a[MSIZE],int s,char** b);
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MSIZE 10
void new_gen(char * a[MSIZE],int s,char** b); /*the function we talked about*/
void print_m(char** b,int s); /*prints matrix*/
void cpy_m(char** b, char** a, int s);
int main()
{
int Size, gen, i, j;
printf("Enter number of generations\t");
scanf("%d", &gen);
printf("\nEnter size of the matrix (max size is %d and min is 2)\t", MSIZE);
scanf("%d", &Size);
char **m = (char**) malloc(Size*sizeof(char*));
for (i=0; i<Size; i++)
{
m[i] = (char*) malloc(Size*sizeof(char));
}
printf("Enter matrix of first generation\n");
for (i=0; i<Size; i++) {
for (j=0; j<Size; j++) {
scanf(" %c", &m[i][j]);
}
}
print_m(m, Size);
for (i=1; i<gen; i++)
{
char **n = (char**) malloc(Size*sizeof(char*));
for (i=0; i<Size; i++)
{
n[i] = (char*) malloc(Size*sizeof(char));
}
new_gen(m, Size, n);
print_m(n, Size);
cpy_m(n, m, Size);
}
return 0; }
void print_m(char** b, int s)
{
int i, j;
putchar('\n');
for (i=0; i<s; i++)
{
for (j=0; j<s; j++) {
printf("%c", *(*(b+i)+j));
}
putchar('\n');
}
return;
}
void cpy_m(char* b[MSIZE],char** a, int s)
{
int i, j;
for (i=0; i<s; i++) {
for (j=0; j<s; j++) {
*(*(a+i)+j) = b[i][j];
}
return;
}}
#包括
#包括
#包括
#定义MSIZE 10
无效新生成(字符*a[MSIZE],整数s,字符**b)/*我们讨论的功能*/
无效打印(字符**b,整数s)/*打印矩阵*/
无效cpy_m(字符**b,字符**a,整数s);
int main()
{
整数大小,gen,i,j;
printf(“输入代数\t”);
scanf(“%d”和“&gen”);
printf(“\n矩阵的输入大小(最大大小为%d,最小为2)”,MSIZE);
scanf(“%d”,大小(&S);
char**m=(char**)malloc(Size*sizeof(char*));
对于(i=0;i考虑这对嵌套循环
for (i=1; i<gen; i++)
{
char **n = (char**) malloc(Size*sizeof(char*));
for (i=0; i<Size; i++)
{
n[i] = (char*) malloc(Size*sizeof(char));
}
new_gen(m, Size, n);
print_m(n, Size);
cpy_m(n, m, Size);
}
for(i=1;iSo)你的问题到底是什么?我看到了你的一般问题陈述,也看到了你的代码。你具体想知道什么?什么不起作用?