I get the following error with the following code. I tried to figure out where the problem is over Google, but I didn't find anything helpful.
Compiling /home/tectu/projects/resources/chibios/ext/lcd/touchpad.c
In file included from /home/tectu/projects/resources/chibios/ext/lcd/touchpad.c:1:0:
/home/tectu/projects/resources/chibios/ext/lcd/touchpad.h:17:1: warning: useless type qualifier in empty declaration [enabled by default]
Here's the the code from line 12 to line 17 from touchpad.h
:
volatile struct cal {
float xm;
float ym;
float xn;
float yn;
};
And here's how I use this struct inside touchpad.c
:
static struct cal cal = {
1, 1, 0, 0
};
Can anyone show me the light? :D
volatile
as a qualifier can be applied to a particular instance of structure.
You are applying it to a type which is useless and the compiler correctly points it out.
volatile
qualifies a variable, not a type.
Doing:
static volatile struct cal {
float xm;
float ym;
float xn;
float yn;
} cal;
would be legal, as would:
struct cal {
float xm;
float ym;
float xn;
float yn;
};
static volatile struct cal cal;
The volatile
keyword makes sense with an object. Not a type definition.
You don't get an error, just a warning.
And that applies to how you declare your struct cal
: it is not volatile by itself; the volatile only applies to a concrete variable definition.
So in static struct cal cal
, your variable cal
is just static
, but not volatile
.
In that sense, the volatile
declaration is, as the warning says, useless.
易失性关键工作应该与实际变量一起使用,而不是类型定义。
You can't attach a volatile
qualifier to a struct
declaration.
But, contrary to the other answers, you can in fact use the volatile
qualifier on types, but not on structs . If you use typedef
, you can create a volatile type for a struct.
In the following example, Vol_struct
is actually not volatile, as in the poster's question. But Vol_type
will create volatile variables without further qualification:
/* -------------------------------------------------------------------- */
/* Declare some structs/types */
/* -------------------------------------------------------------------- */
/* wrong: can't apply volatile qualifier to a struct
gcc emits warning: useless type qualifier in empty declaration */
volatile struct Vol_struct
{
int x;
};
/* effectively the same as Vol_struct */
struct Plain_struct
{
int x;
};
/* you CAN apply volatile qualifier to a type using typedef */
typedef volatile struct
{
int x;
} Vol_type;
/* -------------------------------------------------------------------- */
/* Declare some variables using the above types */
/* -------------------------------------------------------------------- */
struct Vol_struct g_vol_struct; /* NOT volatile */
struct Plain_struct g_plain_struct; /* not volatile */
volatile struct Plain_struct g_vol_plain_struct; /* volatile */
Vol_type g_vol_type; /* volatile */
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.