Virtual Wire -> cannot convert 'string' to 'const char*' in initalization

I am sending string data through virtual wire but I need to convert it to const char first. It says I cannot.
Here is transmitter code.

#include <VirtualWire.h>
#include <Time.h>
#include <TimeAlarms.h>
#include <stdio.h>
#include <string>
#undef int
#undef abs
#undef double
#undef float
#undef round

//--------
//This program measures waterflow and outputs in L/hour every second
#define sensor 2
volatile int NbTopsFan; //Creates everchanging variable
int Calc; //Creates integer variable
void rpm() //The function the interrupt calls
{
  NbTopsFan++;
}
//--------


void setup()
{
  Serial.begin(2400);
  vw_set_tx_pin(12);
  vw_setup(2000);
  //-------
  pinMode (2, INPUT); //Initiates Pin 2 as input
  attachInterrupt (0,rpm,RISING);//calls rpm function from pin 2
  
}

void loop()
{
  //---------
  NbTopsFan = 0; //Sets variable to 0
  sei(); //Enables interrupts
  delay (300); //Tells Arduino to wait in milliseconds.
  cli(); // Disables interrupts
  Calc = (NbTopsFan * 60 /7.5); //(Pulse Frequency x 60)/7.5 = flowrate
  String calcstring=String(Calc);
  String data=(calcstring+"L/hour\r "+hour()+":"+minute()+":"+second()+"\n");
  Serial.print (data); //Prints number calculated above
  //---------
  
  const char*msg=data;
  //digitalWrite(13, true);
  vw_send((uint8_t *)msg, strlen(msg));
  
  //digitalWrite(13, false);
  //delay(1000); //how much time do you to give it to send msg
}

Here is receiver code

#include <VirtualWire.h>
#undef int
#undef abs
#undef double
#undef float
#undef round
void setup()
{
  Serial.begin(2400);
  Serial.println("setupreceiver");
  vw_set_rx_pin(5);
  vw_setup(2000);
  
  vw_rx_start();
}

void loop()
{
  uint8_t buf[VW_MAX_MESSAGE_LEN];
  uint8_t buflen = VW_MAX_MESSAGE_LEN;
  if (vw_get_message(buf, &buflen))
  {
    String res=((const char*)buf);
    res.trim();
    Serial.print(res); 
    Serial.println("");
    vw_wait_rx();
    //digitalWrite(13,false);
  }
}

cannot convert 'string' to 'const char*' in initalization

Yes, it can.

What it can't do is convert a String to an array of chars, automatically. You must do that yourself.

Perhaps the toCharArray() method of the String class would be useful. Or, even better, ditch the String class altogether, and use sprintf() and a char array.

the reason why I want to convert my string data to const char because I need it for this line

const char*msg=data;

Usually, in the example of virtual wire, it is

const char*msg="hello";

So pretty much im changing the "hello" to a different string, which a variable called "data".

Sorry, I am still new to arduino.

the reason why I want to convert my string data to const cha

Learn to love your "shift" key.
string != String

my data is String

See reply #1

I used the following line,

data.toCharArray(data,30);

and the error i get is

no matching function for call to 'String::toCharArray(String*,int)

so toCharArray requires a buf and length, it does not seem to work however

well you are really trying to copy the data to itself there, so it is not going to work.

If you want to copy the data from an object of the String class, into an array of characters,
you would need something like this.

String s = String("some string here");   // not sure if this is right, I don't use arduino strings
char buf[30] ;                                  // an array of chars to put the string into.   
s.toCharArray(  buf,  30 );                 // copies the chars from the string s  into the char array

okay here is the code,

String data=String("L/hour\r");
char buf[30];
data.toCharArray(buf,30);
const char*msg=data; //<--- error is here
vw_send((uint8_t *)msg,strlen(msg));

I get the same error I have in my subject line.

const char*msg=data; //<--- error is here

Sure it is. You extracted the array of data you want to send from the String instance (data) into buf. So, why are you not pointing to buf? You know that buf is already a pointer, right? And that you don't need ANOTHER pointer.

Thank you PaulS, AWOL and michinyon!!!