Struct defined in the file.auxiliary c (with Function Definition and structs) is not recognized in main

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 containing the main.

No .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’ "

Both na struct creation, when in the creation of the pointer to it.

Note: I'm 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?

Author: Lucas Pletsch, 2016-07-15

1 answers

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

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

Define means to give a body to the 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 file .C, while statements are usually placed in file .h. This is necessary because whenever you are going to use a definition, you have to see the statement.

So in your case the statement of ESTRUCT 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. Therefore, you must declare it in a file .H and include this file where is the function main which by the way, is where you make a definition when writing:

ESTRUCT vetor;
 0
Author: , 2016-07-15 17:16:02