On Wed, 30 May 2007, Geert Uytterhoeven wrote:
>
> Apparently you get a warning only if the _first_ occurrence of a struct
> is declared inside a parameter list of a function.
This is normal C behaviour (and afaik, the "inside a parameter list" part
is actually just _modern_ C, not traditional C. I _think_ traditional C
had a single global namespace for struct names, and no scoping at all).
You can use a _pointer_ to a structure without declaring the structure
itself, and this is totally standard C behaviour. The very use of the
pointer will tell the C compiler that such a structure exists, and since
you only use the pointer, the compiler doesn't care what the struct
_looks_ like. It just adds it to its list of known structures.
In fact, you can often use such a pointer without _ever_ declaring the
structure at all. The structure may not even _exist_. It may be just a way
to get type safety, and every time you want to actually use the pointer,
you have to explicitly cast it to something else.
(Example: in <stdio.h> you can do
typedef struct dummy_made_up_struct FILE;
and make sure that everybody ever uses a "FILE *f", and trying to create a
"FILE f" would be a compile-time error, because that "struct
dummy_made_up_struct" simply doesn't even exist, and all the real users
will cast it to some internal thing)
The reason the "inside a parameter list" is special is that modern C (I
think through some obscure C++ reason, but I'm not sure) considers the
function parameter list to be inside function scope, and that includes the
types, not just the parameter names.
So when you do
extern function(struct hello *);
without (forward-)declaring "struch hello" earlier, then the compiler will
still create a "struct hello" internally, but since the scope of that
declaration is purely just that function prototype itself, it will be
forgotten immediately afterwards, and a subsequent use of "struct hello"
will be considered a _different_ "struct hello", since it's in different
scope.
So you generally never need to forward-declare structures, unless you only
then use them inside function prototypes. In which case you would just do
struct hello;
to tell the compiler that you have a "struct hello" that you haven't
actually declared yet, and now it's in scope _outside_ the function
prototype, and everybody is happy and agrees that they are using the same
"struct hello".
Linus
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
[Index of Archives]
[Kernel Newbies]
[Netfilter]
[Bugtraq]
[Photo]
[Stuff]
[Gimp]
[Yosemite News]
[MIPS Linux]
[ARM Linux]
[Linux Security]
[Linux RAID]
[Video 4 Linux]
[Linux for the blind]
[Linux Resources]