PoleAco
PoleAco

Reputation: 23

Proper way to declare and use structures in C project?

I am building a project that I am trying to organize as follows:

main.c
globals.h
structures.h
FunctionSet1.c, FunctionSet1.h
FunctionSet2.c, FunctionSet2.h
etc.

I thought I could define a structure type in structures.h:

struct type_struct1 {int a,b;}; // define type 'struct type_struct1'

then declare a function1() returning a structure of type type_struct1 in FunctionSet1.h:

#include "structures.h"
struct type_struct1 function1(); // declare function1() that returns a type 'struct type_struct1'

then write function1() in FunctionSet1.c:

#include "FunctionSet1.h"
struct type_struct1 function1() {
  struct type_struct1 struct1; // declare struct1 as type 'struct type_struct1'
  struct1.a=1;
  struct1.b=2;
  return struct1;
}

Edit: with the corrected code above, the compiler returns

306 'struct' tag redefined 'type_struct1' structures.h

Is the file set good practice ? What is the good practice to manage the structures ?

Upvotes: 1

Views: 184

Answers (4)

PoleAco
PoleAco

Reputation: 23

Thank you all.

I read again what you said and found that the code above is now correct. The error I report is with testing the following main.c

#include "structures.h"
#include "FunctionSet1.h"
void main() {
  struct type_struct1 struct2;
  struct2=function1();
}

in which structures.h is included again, thus causing the error. Removing the include eliminates the error.

I will now look into header guards to avoid such problems.

Thanks again.

Upvotes: 0

dbush
dbush

Reputation: 223992

Your general structure looks good. One thing you need to do, as zenith mentioned, is to put include guards into your header files. What that is is a set of #define's that make sure that the contents of the header are not included more that once in a given file. For example:

structures.h:

#ifndef STRUCTURES_H
#define STRUCTURES_H

struct type_struct1{
    int a,b;
};

...
// more structs
...

#endif

FunctionSet1.h:

#ifndef FUNCTION_SET_1_H
#define FUNCTION_SET_1_H

#include "structures.h"

struct type_struct1 function1();

...
// more functions in FucntionSet1.c
...

#endif

main.c:

#inlcude <stdio.h>
#include "structures.h"
#include "FunctionSet1.h"

int main(void)
{
    struct type_struct1 struct1;
    struct1 = function1();
    return 0;
}

Here, main.c includes structures.h and FunctionSet1.h, but FunctionSet1.h also includes structures.h. Without the include guards, the contents of structures.h would appear twice in the resulting file after the preprocesser is done. This is probably why you're getting the "tag redefined" error.

The include guards prevent these type of errors from happening. Then you don't have to worry about whether or not a particular header file was included or not. This is particularly important if you're writing a library, where other users may not know the relationship between your header files.

Upvotes: 0

Valentin Duboscq
Valentin Duboscq

Reputation: 978

First of all, you have to declare the structure in your file.h (you can use typedef to create an alias)

   typedef struct   Books
   {
      char          title[50];
      int           book_id;
   } books; 

then, you have to include your file.h in your file.c and declare your variable like this

#include "file.h"

int       main()
{
    books book1;

    book1.title = "Harry Potter";
    book1.book_id = 54;
}

or like this if you didn't use typedef

#include "file.h"

int              main()
{
    struct Books book1;

    book1.title = "Harry Potter";
    book1.book_id = 54;
}

Upvotes: 0

Joyas
Joyas

Reputation: 433

In your example, you declare a structure named type_struct in structure.h, then in FunctionSet1.h the structure that you are returning is type_struct, and in the .c it is called struct1.

So i think that the problem is that struct1 and type_struct are not recognized because they have never been defined ...

However, the organization of your files is fine.

Upvotes: 1

Related Questions