Search code examples
ckeywordvolatile

C - use of a volatile pointer


Why would one create a volatile pointer? And suppose I want a volatile pointer which points to a volatile variable, which of the following declarations would accomplish this:

volatile int *pData;

or

volatile int * volatile pData;

Solution

  • Why would one create a volatile pointer?

    Example: To access data whose pointer is updated by a background process.

    Stuff * volatile VideoFrame;
    for (;;) {
      Block_Changes();
      Stuff MyCopy = *VideoFrame;
      Allow_Changes();
      Use(&MyCopy);
    }
    

    I want a volatile pointer which points to a volatile variable, which of the following declarations would accomplish this:

    The 2nd meets the goal. volatile int * volatile pData; is a:
    pData as volatile pointer to volatile int


    The 1st volatile int *pData; is a non-volatile pointer to volatile data:
    pData as pointer to volatile int

    The volitle keyword is most often used in this context. @ Eugene Sh.