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

Asked

Viewed 407 times

1

I’m doing a C project, 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 that struct as follows:

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

    ESTRUCT vetor;

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

But gcc accuses error: "error: Unknown type name ESTRUCT'"

Both in the creation of the struct, and in the creation of the pointer for it.

Note: I am using a Makefile to build the program, follows 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 Makefile. Someone can see it?

  • Ah, the contents of the file. h is this: void initializ_vector(struct vector *v1); struct vector vector vector;

  • Includes placed in the two files . c: the definitions and the main: #include <stdlib. h> #include <stdio. h> #include <pthread. h> #include <string. h>

  • But you are doing include of the file where you set the struct?? It seems that...

  • Cristian, in the file. h I did I put the "prototype" of the struct (I don’t even know if I need it). Which is the following: struct vector nameQualquerDeStruct and in the file. c (without the main) I put the complete struct ( typedef struct estructe{ ..... } ESTRUCT, complete with your content. What do you mean by define? Declare the complete struct?

  • Forget it, I’ve seen the answer. Thanks

1 answer

0

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

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

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

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

The settings are usually placed in a file. c, while the statements are usually placed in a .h. file, this is necessary because whenever you use a definition, you have to see the statement.

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

ESTRUCT vetor;
  • I get it, but in my file that contains MAIN, what is the syntax to include my . h? Is it the same as the . h of the language? = Ex: #include <stdio. h> I tried this with my . h, but it gave "fatal error".

  • Try #include "arquivo.h"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.