0

I'm not understanding how to apply what my teacher is telling me.

What my teacher is telling me is if you want to declare a struct in the .h file that you are making the struct public, but if you declare the struct in the .c implementation file then you are making it private. However, if I declare it in my .c file and then try and put the two together in a main.c file, I always get errors, and if I move that declaration back to the .h file, then everything works out. The error I get is always a forward declaration error from declaring in the .c file

So, can I put the struct declaration (via pointers) in my .c file, and how would I go about it?

TLDR ; I'm not understanding how to declare structs in my .c implementation file with a pointer in conjunction with my .h file.

edit:

Here would be the .h file

typedef char *ListItemP;
typedef struct List *ListP;

//some functions

And here would be the .c file

struct List
{
    int foo;
    ListItemP P;
};

//implementations of .h functions

However, whenever I do as I typed above I always get errors and I just can't figure out why to save my life.

like9orphanz
  • 55
  • 1
  • 9
  • Just declare it like anything else – Ryan Sep 30 '14 at 23:35
  • We're not mind readers. We need to see some code. – Carey Gregory Sep 30 '14 at 23:37
  • Could you update your question with a small example what you are trying to do? There are several ways to declare types but which one to use will depend on what you want to do. – 5gon12eder Sep 30 '14 at 23:38
  • If the main program needs to know about the internals of the structure, the structure needs to be defined in the header so that both the main code and the implementation code can use it. If the main program only needs a pointer to the structure type and doesn't need to know about the internals, then you can hide it in the implementation code. (There are several closely related questions, some from this week, and some from about a year ago, and some from several years ago -- the topic comes up each year as people start learning C.) – Jonathan Leffler Sep 30 '14 at 23:39
  • I think your teacher is talking about P-IMPL idiom aka Opaque pointer aka Cheshire cat. You'll find your answer in the duplicate question above, or just Google them. – Gonen I Sep 30 '14 at 23:49
  • Where's the #include directive in the C file? – keshlam Oct 01 '14 at 00:08
  • I think you should be more specific about what errors you receive. I'm voting not to reopen in the meantime. – Mike Oct 01 '14 at 00:15

2 Answers2

0

You haven't provided any code, so I can't speak to your specific circumstance.

Putting something in a .h or .c file, by itself, does not affect its public or privateness. However, by putting it in a .h file that gets #included into 2 different .c files (which is what I'm assuming you did) means that those files will use identical definitions.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

Declarations in a .h file can be reference by multiple .c files. That's very much what you want when declaring functions, structs, and constants that will be used by code in other .c files to call functions in the one you're writing; if the two sets of code don't agree on these definitions, calls between them won't work as intended.

You do, of course, have to make sure that the .h file is self-contained and includes everything that will be needed to compile everything inside it -- except possibly other .h files, though it's more common to have a .h issue its own includes for the things it needs. (Which raises the possibility of having the same .h included more than once, which is why most are written with "include guards" that ensure their contents will appear only once no matter how many times they're included.)

keshlam
  • 7,931
  • 2
  • 19
  • 33