请问这个应该怎么做?要求需要使用 malloc 和 free。第一,二张是要求,第三张是机翻。求大lao忙帮,蟹蟹。
#include <stdio.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
// TODO ABOVE
// TODO BELOW
// part2: ensure the argument we're interested is in of right length
// TODO ABOVE
// TODO BELOW
// part3: malloc your 2D array and populate it
// 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++;
}
}
代码如下:如有帮助,请采纳一下,谢谢。
#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++;
}
}