Skip to contentSkip to conversation

Structure declaration without its members variables

DISCUSSION harshkdev 3 participants 3 messages Apr 26, 2015 – Apr 27, 2015

lore
Share & tools
lore

harshkdev

author
#0 lore raw

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

#1 lore raw
Quoted 4 lines · harshkdev

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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 848 bytes
Desc: not available
Url : http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150426/def821f9/attachment.bin

#2 lore raw
Quoted 24 lines

-----Original Message-----
From: kernelnewbies-bounces at kernelnewbies.org [mailto:kernelnewbies- bounces at kernelnewbies.org] On Behalf Of Valdis.Kletnieks at vt.edu
Sent: Sunday, April 26, 2015 7:23 PM
To: harshkdev
Cc: kernelnewbies at kernelnewbies.org
Subject: Re: Structure declaration without its members variables

Quoted 4 lines · harshkdev

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;
}
Quoted 8 lines
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 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[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