Struct defined in the .c auxiliary file (with definition of functions and structs) is not recognized in main

1

I'm doing a project in C, and I have 3 files:

1 .c containing the definitions of functions and structs.

1 .h containing the prototypes of these functions and structs

ae1 .c that contains main.

In the .c with the definitions, I defined the struct:

typedef struct vector {

    int numDePosicoes;
    double vetor[5000];

}ESTRUCT;

And in the main method, I try to create an instance of this struct as follows:

int main(int argc,char* argv[]) {

    ESTRUCT vetor;

    //criando separadamente um ponteiro para a struct
    ESTRUCT *ponteiroPraVetor = &vetor;

But gcc accuses the error: "error: unknown type name 'ESTRUCT'"

In both the creation of the struct, and in the creation of the pointer for it.

Note: I am using a Makefile to mount the program, follow it below:

CFLAGS=-Wall

Roteiro5exe:    mainr5.o    Roteiro5.o

mainr5.o:   mainr5.c    Roteiro5.h

Roteiro5.o: Roteiro5.c  Roteiro5.h

clean:
    rm *.o

NOTE: When I put all the code in the same file, and simply compile it, it works. Maybe the problem is in the makefile. Can anyone see it?

    
asked by anonymous 15.07.2016 / 17:05

1 answer

0

It seems to me that you are confusing statement with definition. Whenever you want to use something, you have to declare it. Declaring means introducing something to the system. For example:

extern int a;
typedef struct { int a;} X;
void print();

Defining means giving a body for statements. You can imagine how to create a space in memory, where the statements will live. For example:

int a = 1;
X k;
void print() {}

Definitions are usually placed in a .c file, whereas declarations are usually placed in an .h file. This is necessary because whenever you use a setting, you have to see the statement.

That's why, in your case, the ESTRUCT statement is in the wrong place. This is not a definition, it is a statement and it should be seen in all files where you want to use it. So you should declare it in a .h file and include this file where the main function is, which by the way, is where you make a definition when writing:

ESTRUCT vetor;
    
15.07.2016 / 18:56