Why should I declare a non-essential variable struct file_handle before I can use this type?

Following the documentation for Linux open_by_handle_at

():

http://man7.org/linux/man-pages/man2/open_by_handle_at.2.html

I am writing this C file:

#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

typedef void (*foobar) (struct file_handle *);

      

but it compiles with an ominous warning:

>gcc -c foobar.c
warning: ‘struct file_handledeclared inside parameter list

      

If I add an inappropriate declaration between:

#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

struct file_handle *junk;

typedef void (*foobar) (struct file_handle *);

      

then it compiles without warning. Why warning?

+3


source to share


1 answer


You have not announced struct file_handle

anywhere in advance. The compiler sees struct file_handle

in the above function definitions for the first time. In each case, the declaration struct file_handle

is block-scoped, that is, local to the corresponding function.

Add below structure before your function.



 struct file_handle {
                   unsigned int  handle_bytes;   /* Size of f_handle [in, out] */
                   int           handle_type;    /* Handle type [out] */
                   unsigned char f_handle[0];    /* File identifier (sized by
                                                    caller) [out] */
               };

      

The higher level language allows you to place your declarations / definitions anywhere (like C ++ or Python), but unfortunately C is compiled from top to bottom

0


source







All Articles