Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   declaration issues (http://www.go4expert.com/forums/declaration-issues-t8312/)

technosavvy 18Jan2008 12:45

declaration issues

I have just come across an issue that i am just not able to understand..
i have declared a structure in one of .c files ..


struct fun {
char var1;
int var 2;
struct fun *next

now what i have done is i have made a header file and put a typedef like:


typedef struct fun morefun
and GOD KNOWS how the header file is able to know that the structure has been defined in the other file..and code is compiling with not even a single warning..

Salem 18Jan2008 17:11

Re: declaration issues
The typedef doesn't do anything by itself, except create an alias for some other kind of declaration (in this case, a struct).

For example, you can also put this in your header file.
struct fun;

Also, it's possible to declare pointers to structures without the body of the structure in scope, so for example
struct fun *handle;
This ability allows you to create an interface to your package/module which has an opaque type. That is, the user only deals with a pointer-to-something, but can never dereference it to sneakily change something on the inside. It's like how the FILE* type for handling files in stdio works.

You only need the actual structure in scope when you create variables with that structure, eg.
struct fun myvar;
or when you have a pointer to the struct, and you want to dereference the pointer, eg.
ptr->member = value;

technosavvy 19Jan2008 21:05

Re: declaration issues
salem...i believe the uestion was not put in an elucid manner...or maybe i have not understood the answer...

what i was tying to ask was that how come the header file knows that there is a declaration of that structure in some other file ...
please expain in a bit elaborative way...:)

Salem 19Jan2008 23:17

Re: declaration issues
It doesn't know, nor does it need to, at that point in the compilation process.

Like say extern variables and function prototypes tell the compiler about data and functions which are to be declared later, all you're doing is saying that 'morefun' is an alias for 'struct fun'. At that point, you don't need to know any more detail than that.
It's only when you actually USE the declaration (to declare an instance of that struct say) that you need the complete declaration in scope.

Consider a recursive structure.

// this is no different to your typedef
struct bar;

struct foo {
  struct bar *next;  // not an instance, only a pointer
struct bar {
  struct foo *next;

Because foo only has a pointer to bar, it doesn't need the whole struct in scope to be able to declare foo.

oogabooga 19Jan2008 23:58

Re: declaration issues

Originally Posted by technosavvy
please expain in a bit elaborative way...:)

I found Salem's answer very informative.
However, if you put this at the top of a working program it will not compile:
typedef DoesNotExist AnotherNameForDoesNotExist;
But this will:
typedef struct DoesNotExist AnotherNameForDoesNotExist;
So it is really about the "struct" keyword and not "typedef".
To some degree, all structs are apparently considered to be of the same "type".
This might relate to C's separate struct namespace, which always seemed kind
of weird (useless) to me, but there must be some reason.

As for FILE, in MinGW's stdio.h it is declared like this:

/* The structure underlying the FILE type.
 * I still believe that nobody in their right mind should make use of the
 * internals of this structure. */

typedef struct _iobuf
    char* _ptr;
    int  _cnt;
    char* _base;
    int  _flag;
    int  _file;
    int  _charbuf;
    int  _bufsiz;
    char* _tmpfname;

So you could get at it, but there's really no point.

All times are GMT +5.5. The time now is 22:02.