John Ben
John Ben

Reputation: 1

I get an error I'm new to structures and don't know how to solve it

I get this error when I try to compile it :

‘struct TMatrix’ declared inside parameter list will not be visible outside of this definition or declaration

3 | int InputMatrix (struct TMatrix Matrix);

#include <stdio.h>
#define MAX 1000
int InputMatrix (struct TMatrix Matrix);
struct TMatrix{
    int L, C;
};
int main() {
    struct TMatrix Matrix;
    InputMatrix(Matrix);
    return 0;
}

int InputMatrix (struct TMatrix Matrix) {
}

Upvotes: 0

Views: 45

Answers (1)

Vlad from Moscow
Vlad from Moscow

Reputation: 310980

The scope of the declaration of the structure struct TMatrix within the function declaration

int InputMatrix (struct TMatrix Matrix);

is the parameter list. Outside it this declaration is invisible.

So this structure declaration

struct TMatrix{
    int L, C;
    int N;
    int LIN[MAX];
    int COL[MAX];
    float X[MAX];
};

declares another structure in the file scope.

You need to exchange the declarations like

struct TMatrix{
    int L, C;
    int N;
    int LIN[MAX];
    int COL[MAX];
    float X[MAX];
};

int InputMatrix (struct TMatrix Matrix);

Or before the function declaration to place a forward declaration of the structure

struct TMatrix;

int InputMatrix (struct TMatrix Matrix);

Pay attention to that if you want to change the object of the structure type declared in main then you need to pass it to the function through a pointer to it. Otherwise the function parameter does not make a sense. That is the function should be declared like

void InputMatrix( struct TMatrix *Matrix );

and in main you should write

struct TMatrix Matrix;
InputMatrix( &Matrix );

Upvotes: 1

Related Questions