Structure declaration without its members variables

Jeff Haran Jeff.Haran at
Mon Apr 27 12:47:38 EDT 2015

> -----Original Message-----
> From: kernelnewbies-bounces at [mailto:kernelnewbies-
> bounces at] On Behalf Of Valdis.Kletnieks at
> Sent: Sunday, April 26, 2015 7:23 PM
> To: harshkdev
> Cc: kernelnewbies at
> Subject: Re: Structure declaration without its members variables
> On Mon, 27 Apr 2015 00:16:59 +0530, harshkdev said:
> > struct kvm;
> > struct kvm_vcpu;
> >
> > Generally we declare structure and its member at same place.
> It's a forward declaration.
> Consider two structures that have pointers to each other:
> struct a {
>       int b, c, d;
>       struct *b b_ptr;
> }
> struct b {
> 	int foo, bar, baz;
> 	struct *a a_ptr;
> }
> Now, the struct *b in the first structure won't compile because it hasn't seen
> b yet.  So we stick a 'struct b;' in front both of them so struct a can compile
> successfully.

This does not seem to be true, though I had to fiddle with the definitions of structs a and b to get gcc to not generate syntax errors:

[jharan at js1]~/dev/c_fun/3$ cat main.c
#include <stdio.h>

struct a {
      int b, c, d;
      struct b *b_ptr;

struct b {
        int foo, bar, baz;
        struct a *a_ptr;

int main(int argc, char *argv[])
        struct a a1;
        struct b b1;

        printf("address of a1 %p, address of b1 %p\n", &a1, &b1);
        return 0;
[jharan at js1]~/dev/c_fun/3$ gcc main.c
[jharan at js1]~/dev/c_fun/3$ ./a.out
address of a1 8047c90, address of b1 8047c80
[jharan at js1]~/dev/c_fun/3$ type gcc
gcc is hashed (/usr/local/bin/gcc)
[jharan at js1]~/dev/c_fun/3$ gcc --version
gcc (GCC) 3.4.6
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO

[jharan at js1]~/dev/c_fun/3$

I've seen forward declarations used when the definition of struct a is in one .h file and the definition of struct b is in another and you have source code that wants to declare an instance of struct a or b without needing to include both include files.

Jeff Haran

More information about the Kernelnewbies mailing list