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); '' –