> -----Original Message-----
> From: [email protected] [mailto:kernelnewbies-
> [email protected]] On Behalf Of [email protected]
> Sent: Sunday, April 26, 2015 7:23 PM
> To: harshkdev
> Cc: [email protected]
> 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@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@js1]~/dev/c_fun/3$ gcc main.c
[jharan@js1]~/dev/c_fun/3$ ./a.out
address of a1 8047c90, address of b1 8047c80
[jharan@js1]~/dev/c_fun/3$ type gcc
gcc is hashed (/usr/local/bin/gcc)
[jharan@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
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[jharan@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


_______________________________________________
Kernelnewbies mailing list
[email protected]
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

Reply via email to