简体   繁体   English

函数矩阵中的麻烦

[英]Troubles with matrices as arguments in functions

I want to pass a matrix into a function and then return the same matrix to another function. 我想将一个矩阵传递给一个函数,然后将相同的矩阵返回给另一个函数。 This is part of my code: 这是我的代码的一部分:

int** inizializzazione_matrice(int matrice1[][MAX_COLONNE]){
int i = 0, k = 0;

while(i < MAX_RIGHE){
    k = 0;
    while(k < MAX_COLONNE){
        matrice1[i][k] = i;
        k++;
    }
    i++;
}

return matrice1;}

I call this function like that: 我这样称呼这个函数:

stampa_matrice(inizializzazione_matrice(matrice1));

stampa_matrice is a void function that takes the same input of inizializzazione_matrice as argument. stampa_matrice是一个无效函数,将inizializzazione_matrice的相同输入作为参数。

Their declarations are: 他们的声明是:

int** inizializzazione_matrice(int matrice1[][MAX_COLONNE]);
void stampa_matrice(int matrice1[][MAX_COLONNE]);

The error comes out at the inizializzazione_matrice return matrice1; 错误出现在inizializzazione_matrice return matrice1; with the error code: int (*matrice1)[10] Error type value doesn't match the function type . 错误代码: int (*matrice1)[10] Error type value doesn't match the function type

I don't understand why, since the call of inizializzazione_matrice has been done in the same way and it works. 我不明白为什么,因为inizializzazione_matrice的调用已经以相同的方式完成并且可以正常工作。 I tried using the double pointer as argument too, but it's the same and i have an older code in eclipse that works just fine in the same way (that's the scary part). 我也尝试使用双指针作为参数,但这是相同的,并且我在eclipse中有一个较旧的代码,以相同的方式可以正常工作(这是最恐怖的部分)。 Am i missing something? 我想念什么吗? If you need further infos please tell me! 如果您需要更多信息,请告诉我!

You can do it in a simpler way: 您可以通过以下更简单的方式进行操作:

void inizializzazione_matrice(int **matrice1, max_righe, max_colonne)
{
  int i = 0, k = 0;

  while (i < max_righe) {
    k = 0;
    while (k < max_colonne) {
      matrice1[i][k] = i;
      k++;
    }
    i++;
  }
}

No need to return matrice1 , as it is passed by parameter (not by value). 无需返回matrice1 ,因为它是通过参数(而不是通过值)传递的。

You can do it better (code speaking) with two nested for : 您可以使用两个嵌套的嵌套for更好地实现(讲代码):

void inizializzazione_matrice(int **matrice1, max_righe, max_colonne)
{
  for (int i = 0; i < max_righe)
    for (int k = 0; k < max_colonne)
      matrice1[i][k] = i;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM