请问这个应该怎么做?要求需要使用 malloc 和 free。第一要求,第二张是机翻。第三张是错误提示。。然后下面的代码是已经是我写好的代码,但是出现了问题,其中两个可以通过,四个不行。我问了别人然后我没有怎么听明白,所以求忙帮编程1下,蟹蟹。
#include <stdio.h>
#include <stdlib.h>
#define SIZE 3
void printArray(int **arr);
int main(int argc, char *argv[]) {
// TODO BELOW
// part1: ensure the right number of arguments have been passed in
if(argc < 2)
return 0;
// TODO ABOVE
// TODO BELOW
// part2: ensure the argument we're interested is in of right length
int len = 0;
while(argv[1][len] != '\0')
len++;
if(len != 9)
{
printf("length < 9,please input the right numbers\n");
return 0;
}
// TODO ABOVE
// TODO BELOW
// part3: malloc your 2D array and populate it
int **myArray = (int**)malloc(SIZE * sizeof(int*));
for(int i = 0;i<SIZE;i++)
{
myArray[i] = (int*)malloc(SIZE * sizeof(int) );
for(int j = 0;j<SIZE;j++)
myArray[i][j] = (int)(argv[1][i*3+j] - '0');
}
// TODO ABOVE
printArray(myArray);
free(myArray);
return 0;
}
void printArray(int **arr) {
int i = 0;
int j = 0;
while (i < SIZE) {
j = 0;
while (j < SIZE) {
printf("%d ", arr[i][j]);
j++;
}
printf("\n");
i++;
}
}
第25行: int **myArray = (int**)malloc(SIZE * sizeof(int*));
修改为: int **myArray = (int**)malloc(SIZE * sizeof(int));