-2

I'm new in C and i need to return a matrix form a procedure...

I need to write a matrix on the procedure and return it to main and show it.

what i have is this :

int write_matrix()
{
    int matriz[2][2],i,j;

    for(i=0;i<2;i++){
        for(j=0;j<2;j++){
            cin >> matriz[i][j];
        }
    }
    return matriz[][];
}

int main()
{

    int matriz[2][2],i,j;

    matriz[][] = write_matrix(); ????????????????????

    for(i=0;i<2;i++){
        for(j=0;j<2;j++){
            cout << matrix[i][j];
        }
    }

    system("pause");
    return 0;
}
4

1 回答 1

-1

将其作为指针传递

int write_matrix(int matriz[][2])
{

  int i,j;

  for(i=0;i<2;i++){
    for(j=0;j<2;j++){
      cin >> matriz[i][j];
    }
  }
}

int main() 
{  

  int matriz[2][2],i,j;

  write_matrix(matriz); 

  for(i=0;i<2;i++){
    for(j=0;j<2;j++){
      cout << matrix[i][j];
    }
  }
  system("pause");
  return 0;
}

或者,也可以按以下方式声明数组:

int write_matrix(int** matriz)
{

  int i,j;

  for(i=0;i<2;i++){
    for(j=0;j<2;j++){
      cin >> matriz[i][j];
    }
  }
}

int main() 
{  

  int **matriz,i,j;
  matriz = new int *[2];
  for(i = 0; i <2; i++)
    matriz[i] = new int[2];

  write_matrix(matriz); 

  for(i=0;i<2;i++){
    for(j=0;j<2;j++){
      cout << matrix[i][j];
    }
  }
  system("pause");
  return 0;
}
于 2013-06-19T21:59:03.437 回答