1
代码似乎是在正确的轨道上,但它跳过一行并忽略了第一个元素[0,0]将所有元素全部推回到元素后面。动态分配一个二维字符串数组
#include <stdio.h>
#include <stdlib.h>
int main()
{
char **nums;
int i,j,k, num_cases;
int rows, cols;
printf("Enter the number of grids.\n");
scanf("%d", &num_cases);
for(k=0;k<num_cases;k++){
printf("Test Case #%d\n", k+1);
printf("Enter the # of rows & columns separated by a space.\n");
scanf("%d%d", &rows, &cols);
nums = (char**)malloc(rows*sizeof(char*));
for(i=0; i<rows; i++){
nums[i] = (char*)malloc(cols*sizeof(char));
}
printf("Enter your %dx%d grid of letters.\n", rows, cols);
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
scanf("%c", &nums[i][j]);
}
}
}
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
printf("[%d][%d] = %c\n", i, j, nums[i][j]);
}
}
for(i=0; i<rows; i++){
free(nums[i]);
}
free(nums);
return 0;
}
另一个'的scanf( “%d%d”,&行,&cols);用'的scanf( “%d%d”,&行更换,&cols); '' –