代码之家  ›  专栏  ›  技术社区  ›  Yiannis

为什么在某些情况下,此矩阵点积程序在最后一列中打印地址?

  •  1
  • Yiannis  · 技术社区  · 9 年前

    我写了这个程序来求两个矩阵的点积。它适用于几种情况,但当我测试时,我注意到它在最后一列中打印了似乎是地址的内容,我不知道为什么。特别针对以下输入:3 2 3 1 2 1 1 1 2 1 2 3 2 1

    enter image description here

    我正在使用CodeBlocks编译和运行代码。此外,我对C相对较新。

    #include <stdio.h>
    #include <stdlib.h>
    
    //Method to allocate memory for a 2D array
    int** allocateMatrix(int rows, int columns)
    {
        //Declaration of variables
        int **matrix, i;
    
        //Allocate memory to store 'rows' many pointers to integers
        matrix = malloc(rows*sizeof(int*));
    
        //For-loop to allocate memory to store 'columns' many integers, and initialize them to zero
        for (i=0; i<rows; i++)
        {
            matrix[i] = calloc(0,columns*sizeof(int));
        }
    
        return matrix;
    }
    
    int main()
    {
        //Declaration of variables
        int n, m, p; //used as matrix dimensions
        int i, j, k; //used in for-loops
    
        //Read input
        do
        {
            //printf("Enter value of rows for first matrix (greater than 0):\n");
            scanf("%d", &n);
        }while(n<0);
    
        do
        {
            //printf("Enter value of columns for first and rows for second matrix (greater than 0):\n");
            scanf("%d", &m);
        }while(m<0);
    
        do
        {
            //printf("Enter value of columns for second matrix (greater than 0):\n");
            scanf("%d", &p);
        }while(p<0);
    
        //Create three matrices, by calling 'allocateMatrix' function
        int** matrix1 = allocateMatrix(n,m);
        int** matrix2 = allocateMatrix(m,p);
        int** matrix3 = allocateMatrix(n,p);
    
        //Two for-loops to store values in 'matrix1'
        //For-loop to go through rows
        for (i=0; i<n; i++)
        {
            //For-loop to go through columns
            for (j=0; j<m; j++)
            {
                //Read input
                scanf("%d", &matrix1[i][j]);
            }
        }
    
        //Two for-loops to store values in 'matrix2'
        //For-loop to go through rows
        for (i=0; i<m; i++)
        {
            //For-loop to go through columns
            for (j=0; j<p; j++)
            {
                //Read input
                scanf("%d", &matrix2[i][j]);
            }
        }
    
        //THREE for-loops to multiply values in 'matrix1' and 'matrix2' and store results in 'matrix3'
        //For-loop to go through rows of 'matrix3' and 'matrix1'
        for (i=0; i<n; i++)
        {
            //For-loop to go through columns of 'matrix3' and 'matrix2'
            for (j=0; j<p; j++)
            {
                //For-loop to go through columns of 'matrix1' and rows of 'matrix2'
                for(k=0; k<m; k++)
                {
                    //Read input
                    matrix3[i][j] = matrix3[i][j] + (matrix1[i][k] * matrix2[k][j]);
                }
            }
        }
    
        //Print the resulting matrix
        //Two for-loops to print values in 'matrix3'
        //For-loop to go through rows
        for (i=0; i<n; i++)
        {
            //For-loop to go through columns
            for (j=0; j<p; j++)
            {
                if (j==p-1) //for correct format
                {
                    printf("%d", matrix3[i][j]);
                }
                else
                {
                    printf("%d ", matrix3[i][j]);
                }
            }
            //Change line
            printf("\n");
        }
    
        //Free the memory up!!!!
        free(matrix1);
        free(matrix2);
        free(matrix3);
    
        return 0;
    }
    
    1 回复  |  直到 9 年前
        1
  •  2
  •   reader    9 年前

    此处没有分配任何内存:

    matrix[i] = calloc(0,columns*sizeof(int));
    

    calloc的第一个参数设置要分配的元素数。在这种情况下 columns

    matrix[i] = calloc(columns,sizeof(int));
    

    还要确保您验证了scanf输入。