c - 如何将数据输入到C中双指针表示的二维数组中

标签 c arrays

我正在尝试使用终端将数据输入到由双指针表示的二维数组中。

int main() {
    int M, N;

    printf("Please enter the number of rows in the array:");
    scanf("%d", &M);
    printf("Please enter the number of columns in the array:");
    scanf("%d", &N);

    int **A = (int**)malloc(M * sizeof(int *));
    for (int i = 0; i < M; i++)
        A[i] = (int *)malloc(N * sizeof(int));

    // Assigning and printing 2d array
    printf("Please enter the elements in the array:");

    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            scanf("%d", &A[i][j]);      <------- break here
        }
    }

    printf("The array you entered was:");
    for (int i = 0; i < M; i++) {
        for (int j = 0; j < N; j++) {
            printf("%d\t", A[i][j]);
        }
        printf("\n");
    }

    return 0;
}

代码已成功构建,但当我尝试运行它时,程序停止在指定位置:(

This is what it looks like in Xcode This is the terminal

为什么? :(

最佳答案

从插图来看,您似乎已在指示的位置设置了断点。 尝试清除断点并再次运行程序。

关于c - 如何将数据输入到C中双指针表示的二维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60130498/

相关文章:

c - 跳过我的 fgets

c++ - 在C/C++中打开驻留在 “/sys/”下的文件时出错

javascript - 在数组原型(prototype)中循环

iphone - 当Array更改时自动更新UITableView

java - 创建条件语句以对应扫描仪输入

C XML 解析操作

c - C 中数组索引的正确类型是什么?

c - 将源字符串反转为目标字符串

python - 仅从数组中的大元素中减去 10000

arrays - Lisp 为什么我的对象是同一个实例?