All of lore.kernel.org
 help / color / mirror / Atom feed
* [Q] compiler no longer warning about undeclared struct?
@ 2011-07-27 17:57 Guennadi Liakhovetski
  2011-07-28 10:03 ` Bernd Petrovitsch
  0 siblings, 1 reply; 4+ messages in thread
From: Guennadi Liakhovetski @ 2011-07-27 17:57 UTC (permalink / raw)
  To: linux-kernel

Hi

I just ran across a driver in the kernel (drivers/media/video/ov2640.c, 
struct ov2640_priv::info), that does something like

struct xx {
	struct yy *y;
};

static void z(void)
{
	struct xx *x;
	void *p;

	x = ...;
	p = ...;
	x->y = p;
}

where "struct yy" is nowhere declared, and the compiler happily swallows 
this... Shouldn't it complain? Didn't it complain before?

Thanks
Guennadi
---
Guennadi Liakhovetski, Ph.D.
Freelance Open-Source Software Developer
http://www.open-technology.de/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Q] compiler no longer warning about undeclared struct?
  2011-07-27 17:57 [Q] compiler no longer warning about undeclared struct? Guennadi Liakhovetski
@ 2011-07-28 10:03 ` Bernd Petrovitsch
  2011-07-28 10:16   ` Guennadi Liakhovetski
  0 siblings, 1 reply; 4+ messages in thread
From: Bernd Petrovitsch @ 2011-07-28 10:03 UTC (permalink / raw)
  To: Guennadi Liakhovetski; +Cc: linux-kernel

Hi!

On Mit, 2011-07-27 at 19:57 +0200, Guennadi Liakhovetski wrote:
[....]
> I just ran across a driver in the kernel (drivers/media/video/ov2640.c, 
> struct ov2640_priv::info), that does something like
> 
> struct xx {
> 	struct yy *y;
> };
> 
> static void z(void)
> {
> 	struct xx *x;
> 	void *p;
> 
> 	x = ...;
> 	p = ...;
> 	x->y = p;
> }
> 
> where "struct yy" is nowhere declared, and the compiler happily swallows 
> this... Shouldn't it complain? Didn't it complain before?

It's normal C behaviour: As long as the compiler doesn't need the size
or fields of struct yy, it doesn't complain that it doesn't know the
details.

Otherwise you could not define recursive structures as in
----  snip  ----
struct a {
	struct *b;
};
struct b {
	struct *a;
};
----  snip  ----

Kind regards,
	Bernd
-- 
Bernd Petrovitsch                  Email : bernd@petrovitsch.priv.at
                     LUGA : http://www.luga.at


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Q] compiler no longer warning about undeclared struct?
  2011-07-28 10:03 ` Bernd Petrovitsch
@ 2011-07-28 10:16   ` Guennadi Liakhovetski
  2011-07-28 11:08     ` Andreas Schwab
  0 siblings, 1 reply; 4+ messages in thread
From: Guennadi Liakhovetski @ 2011-07-28 10:16 UTC (permalink / raw)
  To: Bernd Petrovitsch; +Cc: linux-kernel

On Thu, 28 Jul 2011, Bernd Petrovitsch wrote:

> Hi!
> 
> On Mit, 2011-07-27 at 19:57 +0200, Guennadi Liakhovetski wrote:
> [....]
> > I just ran across a driver in the kernel (drivers/media/video/ov2640.c, 
> > struct ov2640_priv::info), that does something like
> > 
> > struct xx {
> > 	struct yy *y;
> > };
> > 
> > static void z(void)
> > {
> > 	struct xx *x;
> > 	void *p;
> > 
> > 	x = ...;
> > 	p = ...;
> > 	x->y = p;
> > }
> > 
> > where "struct yy" is nowhere declared, and the compiler happily swallows 
> > this... Shouldn't it complain? Didn't it complain before?
> 
> It's normal C behaviour: As long as the compiler doesn't need the size
> or fields of struct yy, it doesn't complain that it doesn't know the
> details.

I always thought you need forward declarations for those, as in

struct yy;

before declaring struct xx above.

> Otherwise you could not define recursive structures as in
> ----  snip  ----
> struct a {
> 	struct *b;
> };
> struct b {
> 	struct *a;
> };
> ----  snip  ----

I would add a "struct b;" forward declaration before "struct a".

Thanks
Guennadi
---
Guennadi Liakhovetski, Ph.D.
Freelance Open-Source Software Developer
http://www.open-technology.de/

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [Q] compiler no longer warning about undeclared struct?
  2011-07-28 10:16   ` Guennadi Liakhovetski
@ 2011-07-28 11:08     ` Andreas Schwab
  0 siblings, 0 replies; 4+ messages in thread
From: Andreas Schwab @ 2011-07-28 11:08 UTC (permalink / raw)
  To: Guennadi Liakhovetski; +Cc: Bernd Petrovitsch, linux-kernel

Guennadi Liakhovetski <g.liakhovetski@gmx.de> writes:

> I always thought you need forward declarations for those, as in
>
> struct yy;
>
> before declaring struct xx above.

You only need that if you would otherwise introduce the struct tag in a
different scope, like prototype scope (which ends at the end of the
prototype).

>> Otherwise you could not define recursive structures as in
>> ----  snip  ----
>> struct a {
>> 	struct *b;
>> };
>> struct b {
>> 	struct *a;
>> };
>> ----  snip  ----
>
> I would add a "struct b;" forward declaration before "struct a".

You can do that, but since a struct definition doesn't introduce a new
scope it is not necessary.

Andreas.

-- 
Andreas Schwab, schwab@redhat.com
GPG Key fingerprint = D4E8 DBE3 3813 BB5D FA84  5EC7 45C6 250E 6F00 984E
"And now for something completely different."

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2011-07-28 11:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-27 17:57 [Q] compiler no longer warning about undeclared struct? Guennadi Liakhovetski
2011-07-28 10:03 ` Bernd Petrovitsch
2011-07-28 10:16   ` Guennadi Liakhovetski
2011-07-28 11:08     ` Andreas Schwab

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.