As of late, I've been trying to work through opaque pointers as a programming concept and one of the main things I've had difficulties with is figuring out what is or isn't available to other files. In a previous question, I failed in trying to create an opaque pointer to a struct and even though the answer explained how to fix that, I still don't quite understand where I went wrong.
I think that if a struct is defined in file2.c
, file1.c
can use it if both files include header.h
which includes a declaration of the struct? That doesn't entirely make sense to me. header.h
is used by both files, so I can see how they would access the stuff in it, but I don't understand how they would use it to access each other.
When I started programming, I thought it was pretty straight forwards, where you have program files, they can't access anything in each other, and those program files can #include header files with definitions and declarations in them (e.g. file1.c
has access to variables/functions/etc. defined in header.h
). Turns out I was wrong and things are quite a bit more complicated.
So from what I can tell, func()
defined in header.h
can be used by file1.c
without being declared in file1.c
, if file1.c
includes header.h
. As opposed to var
defined in header.h
which needs to be declared in file1.c
with the extern
keyword? And I think if var
is defined in file2.c
, file1.c
can use it if it extern declares it, even if neither file1.c
nor file2.c
include header.h
?
I apologize if the previous paragraphs makes no sense, I'm having quite a bit of difficulty with trying to describe something that confuses me. By all means, please edit this if you are able to fix mistakes or whatnot.
Books and webpages don't seem to help at all. They end up giving me misconceptions because I already don't understand something and draw the wrong conclusions, or they bring up concepts that throw me off even more.
What I'm looking what I'm looking for is an answer that lays this all down in front of me. For example 'this can access this under these circumstances', 'this cannot access this'.