Search code examples
cfunctionredefinition

Getting issues for redefinition of functions


I'm trying to scan numbers into 2 2D arrays, and I keep on getting the error of redefinition.

The code:

#include <stdio.h>
#define N 3

  void getMatrix(double mat[N][N]);
 /*
 char getMenuOption();
 void getCoordinates(int*, int*);
  void sumMatrices(double mat1[][N], double mat2[][N]);
   void changeMatrix(double mat[][N]);
    void printMatrix(double mat[][N]);
*/
int main() {
double A[N][N], B[N][N];
/*
 char option;*/
getMatrix( A[N][N]);
getMatrix( B[N][N]);
/*

option = getMenuOption();*/

return 0;
 }
  void getMatrix(double A[N][N]){
int i;
for(i=0;i<=N;i++){
    for(i=0;i<N;i++)
    {
        scanf("%lf",&A[N][N]);
    }
    }
    return;

 }
    void getMatrix(double B[N][N]){
     int i;
for(i=0;i<=N;i++){
    for(i=0;i<N;i++)
    {
        scanf("%lf",&B[N][N]);
    }
    }
return;
  }

I guess the problem is that the same function is called twice, but im not so sure about it. If anyone can help me point to the problem, it will be most welcome.


Solution

  • The function is defined twice

    First definition

      void getMatrix(double A[N][N]){
    int i;
    for(i=0;i<=N;i++){
        for(i=0;i<N;i++)
        {
            scanf("%lf",&A[N][N]);
        }
        }
        return;
    
     }
    

    Second definition

        void getMatrix(double B[N][N]){
         int i;
    for(i=0;i<=N;i++){
        for(i=0;i<N;i++)
        {
            scanf("%lf",&B[N][N]);
        }
        }
    return;
      }
    

    Take into account that these calls of the function are invalid

    getMatrix( A[N][N]);
    getMatrix( B[N][N]);
    

    The arguments have type double instead of arrays or pointers.

    You should remove one definition of the function and declare the function correctly.

    If the compiler allows to use Variable Length Arrays then the functiuon should be declared like

    void getMatrix(size_t n, double A[n][n]);
    

    if Variable Length Arrays are not supported by the compiler then N must be a constant and the function indeed can be declared like

    #define N SOME_VALUE
    //...
    void getMatrix( double A[N][N] );
    

    and call the function like

    in the first case

    getMatrix( N, A );
    getMatrix( N, B );
    

    and in the second case

    getMatrix( A );
    getMatrix( B );