Eduard
Eduard

Reputation: 11

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.

Upvotes: 0

Views: 1646

Answers (2)

Vlad from Moscow
Vlad from Moscow

Reputation: 310960

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 );

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134316

You don't need to define a function twice (to call it twice or more). One function can be called multiple times, that's the reason of having functions in first place. Get rid of

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;
  }

Having said that, you should call the function like

getMatrix(A);
getMatrix(B);

To pass the array (the decay to pointer, anyway). The notation A[N][N] denotes a member of the array and for an array defined like

double A[N][N];

it's off-by-one, as array indexing in C starts from 0.

Upvotes: 1

Related Questions