简体   繁体   中英

what is purpose of structure declaration and definition here?

I am trying to understand how kernel code is written, following C basics. I encountered that sometimes they do this, (fs.h)

struct backing_dev_info;
struct export_operations;
struct hd_geometry;
struct iovec;

I made a simple C program

 #include <stdio.h>
 struct bdinfo;
 struct bdinfo{
                int a;
                int b;
  };

 int main()
 {
  struct bdinfo var;
  var.a= 3 ;
  var.b = 5 ;
  printf("var.a = %d var.b = %d\n", var.a, var.b);
  return 0;
 }

It works. I need to understand, why struct bdinfo is written in this manner, or in fs.h file? What purpose does it serve in a program to write name of the struct at some place and declare it another place?

This record

struct bdinfo;

is a declaration of a structure without its definition. It introduces a new type declaration in the given scope if it was not yet declared.

It can be used when the exact structure definition is unimportant. For example it can be used in function declarations

struct bdinfo f( struct bdinfo parm1, struct bdinfo parm2 );

or in pointer declarations. For example

struct bdinfo *p;

It makes a header more readable when details do not hide the main.

This record

 struct bdinfo{
                int a;
                int b;
  };

is a structure declaration and at the same time a structure definition. It is required when the exact structure definition is needed.

For example

struct bdinfo s = { 10, 20 };

This declaration requires that the structure definition would be known. Otherwise the compiler can not determine whether the initialization is correct or not.

Take into account that in the given scope there may be several structure declarations and only one structure definition. So you may write for example

 struct bdinfo;

 struct bdinfo{
                int a;
                int b;
  };

 struct bdinfo;

On the other hand if there is the following code

 struct bdinfo;

 struct bdinfo{
                int a;
                int b;
 };

 {
     struct bdinfo;
     //...
 }

then the structure declaration in the blcok scope declares a new structure that hides the declaration of the structure with the same name in the outer scope. They are different structures.

It lets you hide the implementation if you want to. For example:

# foo.h
struct mystr;

struct mystr *foo();
int bar(struct mystr*);

.

# foo.c
#include <stdlib.h>
#include "foo.h"

struct mystr {
  int a;
  int b;
};

struct mystr *foo() {
  struct mystr *s = malloc(sizeof(*s));
  s->a = 1;
  s->b = 2;
  return s;
}

int bar(struct mystr *s) {
  return s->a;
}

.

# bar.c
#include "foo.h"

int main(void) {
  struct mystr *a = foo();
  return bar(a);
}

Here, bar.c is able to use pointers to struct mystr without actually knowing its definition.

Do note however that this is dependent on using pointers. If we had, for example:

# foo.h
struct mystr;

struct mystr foo();
int bar(struct mystr);

Then bar.c wouldn't be able to use foo and bar , since it would need to handle struct mystr objects, for which it needs to know the size of struct mystr , which it can't without knowing its definition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM