Structure declaration without its members variables

2015-04-26 Thread harshkdev
Hi,

What does this syntax means in file
Arch/arm64/include/asm/kvm_asm.h

struct kvm;
struct kvm_vcpu;

Generally we declare structure and its member at same place.

Regards
Harsh Jain
___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


Re: Structure declaration without its members variables

2015-04-26 Thread Valdis . Kletnieks
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.


pgpWOmom2ZFKV.pgp
Description: PGP signature
___
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


RE: Structure declaration without its members variables

2015-04-27 Thread Jeff Haran
> -Original Message-
> From: kernelnewbies-boun...@kernelnewbies.org [mailto:kernelnewbies-
> boun...@kernelnewbies.org] On Behalf Of valdis.kletni...@vt.edu
> Sent: Sunday, April 26, 2015 7:23 PM
> To: harshkdev
> Cc: kernelnewbies@kernelnewbies.org
> 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 

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
Kernelnewbies@kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies