This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Question on volatile keyword

I was wondering why I would receive this error when I add the volatile keyword - I am new to embedded C but my understanding was that it just told the complier not to optimize the variable?

This code give me no errors

void display_text(char *dispString)
{

   char myLcdBuffer[20];
   U32 i;

   my_strcpy(myLcdBuffer, dispString);

   /*
    * Send the data to the LCD screen
    */
   for (i = 0; i < strlen(myLcdBuffer); i++)
  {
     SER_PutCharLcd(myLcdBuffer[i]);
  }

}

However if I add the volatile keyword to the char myLcdBuffer

void display_text(char *dispString)
{

   volatile char myLcdBuffer[20];
   U32 i;

   my_strcpy(myLcdBuffer, dispString);

   /*
    * Send the data to the LCD screen
    */
   for (i = 0; i < strlen(myLcdBuffer); i++)
  {
     SER_PutCharLcd(myLcdBuffer[i]);
  }

}


I get this error msg - error: #167: argument of type "volatile char *" is incompatible with parameter of type "const char *"

Parents
  • Seems only apply to pointers.

    This is fine.

    #include <stdio.h>
    
    void func1(const int var)
    {
        printf("Var is %d\n", var);
    }
    
    int main(void)
    {
        volatile int var_main = 1234;
        func1(var_main);
    
        return 0;
    }
    

    This is not.

    #include <stdio.h>
    
    void func1(const int * var)
    {
        printf("Var is %d\n", *var);
    }
    
    int main(void)
    {
        volatile int var_main = 1234;
        volatile int * ptr_main = &var_main;
        func1(ptr_main);
    
        return 0;
    }
    
    

Reply
  • Seems only apply to pointers.

    This is fine.

    #include <stdio.h>
    
    void func1(const int var)
    {
        printf("Var is %d\n", var);
    }
    
    int main(void)
    {
        volatile int var_main = 1234;
        func1(var_main);
    
        return 0;
    }
    

    This is not.

    #include <stdio.h>
    
    void func1(const int * var)
    {
        printf("Var is %d\n", *var);
    }
    
    int main(void)
    {
        volatile int var_main = 1234;
        volatile int * ptr_main = &var_main;
        func1(ptr_main);
    
        return 0;
    }
    
    

Children