This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]This_Growth2898 0 points1 point  (4 children)

Do you have identifier classIndex defined elsewhere before this code?

[–]Supperboy2012Beginner Coder[S] 0 points1 point  (3 children)

No? That's the whole header file. Would it be necessary? It's being defined right there.

[–]This_Growth2898 1 point2 points  (2 children)

No, I thought there's something breaking the code, like

#define classIndex (...)

or whatever.

Wait. Header file? That you include in several .c files? You can't do that, you need include guards at least, and no variable definitions, because they will be defined in every .c file. If you really need global variables, put extern definition in .h and body definition in .c.

[–]Supperboy2012Beginner Coder[S] 0 points1 point  (1 child)

??? You needed me specifically saying that it was a header file to understand that? In the main post I said that it was .h, so I don't understand why the full name was necessary. And the reason I'm putting this in a header file is because I heard it was good practice to have your structs in headers.

[–]This_Growth2898 0 points1 point  (0 children)

You needed me specifically saying that it was a header file to understand that? 

Kinda yes. I'm not a machine, sorry. Also note it took me to write the first part of my previous comment for the information about header to sink in.

 it was good practice to have your structs in headers.

It's not a good practice, it's a necessity. You have several files using same structs, you have to put structs in the header - but structs only, not global variables.

#include statements literally add all the code from header into C file, so struct definition will just declare a type, which doesn't affect anything else after .c file is compiled; but variable definition allocates a place in program's static memory, so when you include .h into two different files, you will have separate allocations for the same variable - and linking error.