在一个函数传递矩阵(C)的 [英] Passing a matrix in a function (C)

查看:114
本文介绍了在一个函数传递矩阵(C)的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我传递一个矩阵功能在C.还有就是我要创建的函数的一个问题:

 无效插件(INT *矩阵,诠释行,诠释列);

但我注意到,相较于向量,矩阵给我一个错误。我该如何通过我的矩阵功能呢?

编辑 - >还有就是code:

  //矩阵#包括LT&;&stdio.h中GT;
的#define SIZE 100无效插件(INT *矩阵,诠释行,诠释列);
无效打印(INT *矩阵,诠释行,诠释列);诠释的main()
{
    INT垫[SIZE] [SIZE]
    INT行,列;    的printf(输入行:);
    scanf函数(%d个,&安培;排);
    的printf(输入列:);
    scanf函数(%d个,&安培; COL);    的printf(输入数据:\\ n);
    插件(垫,行,列);    的printf(您输入:);
    打印(垫,行,列);    返回0;
}无效插件(INT *矩阵,诠释行,诠释列);
{
    INT I,J;    对于(i = 0; I<排;我++)
    {
        为(J = 0; J<列; J ++)
        {
            的printf(行%d栏%D:,我+ 1,J + 1);
            scanf函数(%d个,&安培;矩阵[I] [J]);
        }
    }
}无效打印(INT *矩阵,诠释行,诠释列)
{
    INT I;
    诠释J;    对于(i = 0; I<排;我++)
    {
        为(J = 0; J<列; J ++)
        {
            的printf(%d个,矩阵[I] [J]);
        }
        的printf(\\ n);
    }
}


解决方案

您必须通过为指针diemensions数,

即。如果你的矩阵是(100 10)2D,那么:

 无效插件(INT **矩阵,诠释行,诠释列);

但previous是错误的,因为任何静态二维数组要求指针的第二个层次是常量指针静态数组。如果矩阵是动态数组的previous头是正确的。因此,正确的是:

 无效插件(INT(*矩阵)[100],诠释行,诠释列);

或你的情况:

 无效插件(INT(*矩阵)[SIZE],诠释行,诠释列);

或者只是:

 无效插件(INT矩阵[10] [100],诠释行,诠释列);

或你的情况:

 无效插件(INT矩阵[SIZE] [SIZE],诠释行,诠释列);

I have an issue passing a matrix to a function in C. There is the function i want to create:

void ins (int *matrix, int row, int column);

but i noticed that in contrast to the vectors, matrix give me an error. How can i pass my matrix to a function so?

EDIT --> there is the code:

// Matrix

#include <stdio.h>
#define SIZE 100

void ins (int *matrix, int row, int column);
void print (int *matrix, int row, int column);

int main ()
{
    int mat[SIZE][SIZE];
    int row, col;

    printf("Input rows: ");
    scanf  ("%d", &row);
    printf("Input columns: ");
    scanf  ("%d", &col);

    printf ("Input data: \n");
    ins(mat, row, col);

    printf ("You entered: ");
    print(mat, row, col);

    return 0;
}

void ins (int *matrix, int row, int column);
{
    int i, j;

    for (i = 0; i < row; i++)
    {
        for (j = 0; j < column; j++)
        {
            printf ("Row %d column %d: ", i+1, j+1);
            scanf  ("%d", &matrix[i][j]);
        }
    }
}

void print (int *matrix, int row, int column)
{
    int i;
    int j;

    for(i=0; i<row; i++)
    {
        for(j=0; j<column; j++)
        {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }
}

解决方案

You must pass the number of diemensions as pointers,

i.e. if your matrix is 2D (10 by 100), then:

void ins (int **matrix, int row, int column);

but the previous is wrong, since any static 2D array requires that second level of pointers be constpointer to static array. The previous header would be correct if the matrix is dynamic array. so the correct is:

void ins (int (*matrix)[100], int row, int column);

or in your case:

void ins (int (*matrix)[SIZE], int row, int column);

or simply:

void ins (int matrix[10][100], int row, int column);

or in your case:

void ins (int matrix[SIZE][SIZE], int row, int column);

这篇关于在一个函数传递矩阵(C)的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆