Skip to content

How to use volatile qualifier with structure?

volatile qualifier with structures in c

In the previous post, we tried understand what a volatile keyword in C/C++ is. In this post, we will have a look at using the volatile keyword with user-defined data types like structure. Some times it is useful to use volatile keyword with user-defined data types. If you need, we have a detailed explanation of Structures in C.

There are different ways of doing this. Each way will help us achieve a different thing. It totally depends on requirements on how to use volatile keyword with structure. Let’s see different scenarios and understand how to use volatile in those scenarios.

Making a single instance of a Structure as Volatile:

Let us consider this code:

typedef struct
{
   unsigned int status;
   unsigned int data;
} myStruct;

//Use of volatile with structure variable

myStruct volatile myStructSens1;
myStruct myStructSens2;

In the above code, we have taken a structure called myStruct and created two instances of that structure called myStructSens1 and myStructSens2 . While creating myStructSens1 instance, we have declared the instance as volatile by using the Volatile Keyword. For myStructSens2 , we didn’t declare it as volatile.

What this does is that only the instance myStructSens1 is made as volatile and not the myStructSens2. Using this approach, we can define specific instances of a structure as volatile. For instance which we do not want to be volatile, we can omit the volatile keyword.

Now, for myStructSens1, all the members of the structure will be volatile. Thus, myStructSens1.status and myStructSens1.data are also volatile.

Making all instances of a Structure as Volatile:

Let us consider this code:

typedef volatile struct
{
   unsigned int status;
   unsigned int data;
} myStruct;

//Use of volatile with structure variable

myStruct myStructSens1;
myStruct myStructSens2;

In the above code, we have taken a structure called myStruct and defined it a volatile. We then created two instances of that structure called myStructSens1 and myStructSens2 .

What this does is that both the instances myStructSens1 and myStructSens2 are volatile. Using this approach, we can define all instances of a structure as volatile.

Warning: Be very careful when using this approach because all the instance will be volatile now.

Now, for myStructSens1 and myStructSens2 , all the members of the structure will be volatile as well. Thus, myStructSens1.status and myStructSens1.data are volatile. And so are myStructSens2.status and myStructSens2.data.

Making specific members of a Structure as Volatile:

Let us consider this code:

typedef volatile struct
{
   unsigned int volatile status;
   unsigned int data;
} myStruct;

//Use of volatile with structure variable

myStruct myStructSens1;
myStruct myStructSens2;

In the above code, we have taken a structure called myStruct and defined it’s member status as volatile. We then created two instances of that structure called myStructSens1 and myStructSens2 .

What this does is that for both the instances myStructSens1 and myStructSens2, only the status variable is volatile. Using this approach, we can define specific members of a structure as volatile.

Now, for myStructSens1 and myStructSens2 , only the variable status will be volatile. Thus, myStructSens1.status and myStructSens2.status are volatile. But myStructSens1.data and myStructSens2.data are non-volatile.

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *