c - 使用固定大小的数组时的垃圾值,而如果使用动态大小的数组,则结果正确

标签 c arrays function

#include<stdio.h>

void readMatrix(int m,int n,int a[][n]);
void displayMatrix(int m,int n,int a[][n]);
void addMatrix(int m,int n,int a[][n],int b[][n]);

int main()
{
    int a[5][5],b[5][5],m,n,p,q;
    printf("Enter the no. of rows and columns of matrix A\n");
    scanf("%d%d",&m,&n);

    printf("Enter the no. of rows and columns of matrix B\n");
    scanf("%d%d",&p,&q);

    if(m!=p||n!=q)
    {
        printf("Matrix addition not possible\n");
        return 0;
    }

    printf("Enter %d elements to matrix A\n",m*n);
    readMatrix(m,n,a);

    printf("Enter %d elements to matrix B\n",m*n);
    readMatrix(p,q,b);

    printf("Matrix A\n");
    displayMatrix(m,n,a);

    printf("Matrix B\n");
    displayMatrix(p,q,b);

    addMatrix(m,n,a,b); 
}

void readMatrix(int m,int n,int a[m][n])
{
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
}
void displayMatrix(int m,int n,int a[m][n])
{
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            printf("%d ",a[i][j]);
        }
        printf("\n");
    }
}


void addMatrix(int m,int n,int a[m][n],int b[m][n])
{
    int c[5][5]; //I guess this is causing problem
    //int c[m][n]; //gives correct answer
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            c[i][j]=a[i][j]+b[i][j];
        }
    }

    printf("Sum Matrix\n");

    /*Gives correct result
    for(int i=0;i<m;i++)
    {
        for(int j=0;j<n;j++)
        {
            printf("%d ",c[i][j]);
        }
        printf("\n");
    }
    */

    displayMatrix(m,n,c);
}



  displayMatrix(m,n,c)是正确的如果我将数组定义为c[m][n],则上面给出了代码。
  
  如果我使用c[5][5],它将给出垃圾值。


我正在使用gcc编译器。我在addMatrix函数本身中显示了总和,这给出了正确的结果。无法找出错误。请帮助。谢谢。

最佳答案

例如,如果m6并且n7在这里会发生什么?

您的循环将到达数组外部的元素。那是不确定的行为。

您会看到,由于数组的大小取决于用户输入的大小,因此纠正错误并在mn上使数组大小保持动态的前瞻性是最好的方法。

因此,如果我理解正确,则希望将值保存在结果矩阵中,然后再使用,例如,使用displayMatrix()打印它。

最简单的方法是在array函数中声明main()并将其作为参数传递给addMatrix()函数,而又不会对代码造成太多干扰。

最后一件事,您应该使用更有意义的名称来命名变量,例如abc,就像名称非常容易混淆一样。

在这里实时查看:

Live sample

#include <stdio.h>

void readMatrix(int m, int n, int a[][n]);
void displayMatrix(int m, int n, int c[][n]);
void addMatrix(int m, int n, int a[][n], int b[][n], int c[][n]);

int main()
{
    int m, n, p, q;
    printf("Enter the no. of rows and columns of matrix A\n");
    scanf("%d%d", &m, &n);
    printf("Enter the no. of rows and columns of matrix B\n");
    scanf("%d%d", &p, &q);

    if (m != p || n != q)
    {
        printf("Matrix addition not possible\n");
        return 0;
    }

    int a[m][n], b[p][q], c[m][n];

    printf("Enter %d elements to matrix A\n", m * n);
    readMatrix(m, n, a);

    printf("Enter %d elements to matrix B\n", m * n);
    readMatrix(p, q, b);

    printf("Matrix A\n");
    displayMatrix(m, n, a);

    printf("Matrix B\n");
    displayMatrix(p, q, b);

    addMatrix(m, n, a, b, c);

    printf("Sum Result\n");
    displayMatrix(m, n, c);
}

void readMatrix(int m, int n, int a[m][n])
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            scanf("%d", &a[i][j]);
        }
    }
}
void displayMatrix(int m, int n, int a[m][n])
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }
}

void addMatrix(int m, int n, int a[m][n], int b[m][n], int c[m][n])
{
    for (int i = 0; i < m; i++)
    {
        for (int j = 0; j < n; j++)
        {
            c[i][j] = a[i][j] + b[i][j];
        }
    }
}

关于c - 使用固定大小的数组时的垃圾值,而如果使用动态大小的数组,则结果正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60071611/

相关文章:

c++ - 当创建所述类型的对象时,静态 (PASS_OBJECT<int>) 模板化数组会丢失数据吗?

java - 在Java中显示图像

javascript - 滚动时更改的 Javascript 菜单的 Else 语句不起作用

postgresql - AFTER INSERT PostgreSQL 触发器的意外行为

c - 查找尾随零数的程序给出了错误的输出

c - 使用 fgets 在 C 中检测 EOL

C 结构 : Initialized Strings become invalid

Java:for循环中的对象数组赋值

c++ - 在声明后使用方法扩展类功能

c - 如何在 C 中将指针结构的二维数组初始化为 NULL?