Search code examples
cpointersdeclarationtypedefvolatile

Qualifier is discarded on struct pointer


I have struct and function declared as follows

typedef struct myStruct
{
    //Some attributes
}myStruct_t, *pMyStruct_t;
void func(myStruct_t* someStruct);

When I declare a struct pointer as follows and pass it to the function, all is fine

volatile pMyStruct_t pStruct;

But when I declare the pointer this way I get an error that the volatile qualifier is discarded in the function

volatile myStruct_t* pStruct;

I guess my question is how come the first method works and no qualifier errors are raised? What is the difference between the two ways the pStruct variable is declared?


Solution

  • These declarations

    volatile pMyStruct_t pStruct;
    

    and

    volatile myStruct_t* pStruct;
    

    are different.

    The first one means the following

    myStruct_t* volatile  pStruct;
    

    That is in the first declaration it is the pointer itself that is volatile while in the second declaration the pointer itself is not volatile.

    You may initialize the function parameter with a value of a pointer that is itself is volatile. But you may not discard the qualifier for the pointed object