无法将值输入分配的矩阵

标签 c malloc

我正在尝试将值添加到我指定的矩阵中,但总是失败,Visual Studio 会提示错误并停止运行。 可以看看我的问题是什么吗?

'#include <stdio.h>
#include <stdlib.h>

void scanMath(int,int,int);
void main()
{
    int  i,n,m,**arr;
    printf("enter the size of the rows\n");
    scanf("%d",&n);
    printf("enter the size of the coloms\n");
    scanf("%d",&m);


    arr=(int**)malloc(n*sizeof(int*));
    for (i=0;i<n;i++)
    {
        arr[i]=(int*)malloc(m*sizeof(int));
    }
    scanMath(arr[n][m],n,m);
    free(arr);
}
void scanMath(int arr,int R,int C)
{
    int i,j;
    for (i=0; i<R; i++)
    {
        for (j=0; j<C; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }
}
    '

最佳答案

#include <stdio.h>
#include <stdlib.h>

void scanMath(int **,int,int);
int main()
{
    int  i,n,m,**arr;
    printf("enter the size of the rows\n");
    scanf("%d",&n);
    printf("enter the size of the coloms\n");
    scanf("%d",&m);


    arr=(int**)malloc(n*sizeof(int*));
    for (i=0;i<n;i++)
    {
        arr[i]=(int*)malloc(m*sizeof(int));
    }
    scanMath(arr,n,m);
    free(arr);
}
void scanMath(int **arr,int R,int C)
{
    int i,j;
    for (i=0; i<R; i++)
    {
        for (j=0; j<C; j++)
        {
            scanf("%d", &arr[i][j]);
        }
    }
}

给你。既然你把它声明为int **arr,而且原型(prototype)是一样的,只传递变量名就够了。

关于无法将值输入分配的矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17069463/

相关文章:

c - 在您的 C 代码中使用数百万个 malloc() 和 free() 的结果?

c - 如何使用 malloc 将 C 中的 int 变量转换为 char *array?

c - 如何将结构类型转换为分配的 char 内存空间

c - 用于写入和读取文件的系统调用

java - 需要澄清 BODMAS 规则

c - FindWindowEx 类/标题帮助

关于 char** 参数的 const char** 参数警告

c++ - 按位运算结果和 bool 值

c - 如何在C语言中正确使用malloc?

c - FreeRTOS 分配错误