You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言矩阵转置程序报错:数组类型不完整等问题求助

解决C语言矩阵转置程序的编译错误

问题描述

我写了一段实现矩阵转置的C语言代码,但编译报错,代码如下:

//transpose of a martrix
// if [1][2] transposed of it is [2][1]
#include<stdio.h>
void transpose(int num[][],int x,int y);
int main()
{
    int x,y;
    int num[10][10];
    printf("What type of matrix you want to working with(x,y)?\n");
    scanf("%d%d",&x,&y);
    //lets the matrix numbers
    for(int i=0;i<x;i++)//for how many row
    {
        for(int j=0;j<y;j++)//for the colume in the matrix
        {
            printf("Data for matrix[%d][%d]:)\n",i,j);
            scanf("%d",&num[i][j]);
        }
    }
    printf("\n");
    //printing the data in the form of the matrix
    /* like [0][0]   [0][1] .....  
             line 27 (printf("\n"));
            [1][0]   [1][1]......*/
    printf("The data in the matrix form is :\n");
    for(int i=0;i<x;i++)
    {
        printf("\n");//this is for the line gap for the martix after a loop is completed.
        for(int j=0;j<y;j++)
        {
            printf("%d\t",num[i][j]);
        }
    }
    transpose(num,x,y);
    return 0;

}

void transpose(int num[][], int x, int y)
{
    int tranpose[10][10];//to stores the trnaspose matrix
    for( int i=0;i<x;i++)
    {
        for(int j=0;j<y;j++)
        {
            tranpose[i][j]=num[j][i];//just exchange the matrix range.eg [1][2 as [2][1]]
        }
    }
    //for the display for the transpose matrix 
    printf("\n");
    printf("The tranpose matrix is :\n");
    for(int i=0;i<x;i++)
    {
        printf("\n");//this is for the line gap for the martix after a loop is completed.
        for(int j=0;j<y;j++)
        {
            printf("%d\t",tranpose[i][j]);
        }
    }

}

编译时出现以下错误:

  • array type has incomplete element type 'int[]'
  • declaration of 'num' as multidimensional array must have bounds for all dimensions except the first

错误原因

C语言规定,传递多维数组作为函数参数时,除第一维可以省略大小,后续所有维度必须明确指定边界。你代码里的void transpose(int num[][],int x,int y);和函数定义中的int num[][]没指定第二维大小,编译器无法确定数组的内存布局,所以报了错。

另外还有个逻辑问题:原矩阵是x行y列,转置后应该是y行x列,但你现在的输出循环还是用x行y列遍历,会导致数组越界或者输出错误内容。


解决步骤

1. 修正函数参数的数组声明

因为你在main里定义的数组是int num[10][10],所以把函数参数的第二维指定为10,修改函数声明和定义:

// 函数声明
void transpose(int num[][10],int x,int y);

// 函数定义
void transpose(int num[][10], int x, int y)
{
    // 原函数内容不变,只改参数
}

2. 修正转置矩阵的输出逻辑

转置后的矩阵行数是原矩阵的列数y,列数是原矩阵的行数x,调整输出循环:

// 替换原输出转置矩阵的循环
printf("\nThe transpose matrix is :\n");
for(int i=0;i<y;i++)  // 行数改为y
{
    printf("\n");
    for(int j=0;j<x;j++)  // 列数改为x
    {
        printf("%d\t",tranpose[i][j]);
    }
}

3. 修正拼写错误

代码里的tranpose是拼写错误,统一改成transpose,避免后续变量名混乱。


完整修正后的代码

// transpose of a matrix
// if [1][2] transposed of it is [2][1]
#include<stdio.h>
void transpose(int num[][10],int x,int y);
int main()
{
    int x,y;
    int num[10][10];
    printf("What type of matrix you want to work with(x,y)?\n");
    scanf("%d%d",&x,&y);
    // input matrix elements
    for(int i=0;i<x;i++)// iterate rows
    {
        for(int j=0;j<y;j++)// iterate columns
        {
            printf("Data for matrix[%d][%d]:\n",i,j);
            scanf("%d",&num[i][j]);
        }
    }
    printf("\n");
    // print original matrix
    printf("The original matrix is :\n");
    for(int i=0;i<x;i++)
    {
        printf("\n");
        for(int j=0;j<y;j++)
        {
            printf("%d\t",num[i][j]);
        }
    }
    transpose(num,x,y);
    return 0;

}

void transpose(int num[][10], int x, int y)
{
    int transpose[10][10];// store transposed matrix
    for( int i=0;i<y;i++)
    {
        for(int j=0;j<x;j++)
        {
            transpose[i][j]=num[j][i];// swap row and column indices
        }
    }
    // print transposed matrix
    printf("\n");
    printf("The transpose matrix is :\n");
    for(int i=0;i<y;i++)
    {
        printf("\n");
        for(int j=0;j<x;j++)
        {
            printf("%d\t",transpose[i][j]);
        }
    }

}

内容的提问来源于stack exchange,提问作者user16904341

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 09:44:59