user7572: I want to write an Arduino program that simply recieves a string (via the I2C wire library) from a master Arduino, then waits for a request, and sends that string back.
Here is my code:
#include <Wire.h>

void setup()
{
  Wire.begin(4); 
  Wire.onReceive(receiveEvent); 
  Wire.onRequest(requestEvent);
}

String data = "";

void loop()
{

}

void receiveEvent(int howMany)
{
  data = "";
  while( Wire.available()){
    data += (char)Wire.read();
  }
}

void requestEvent()
{
    Wire.write(data);
}

I read in the API that the write() function accepts a string, but I keep getting a "No matching function for call" error.  I tried to simply replace
Wire.write(data);

with 
Wire.write("test");

and that worked without error.  Why is this the case? 

user368: data is a String. "test" is a char*. Wire.write() has no prototype that takes a String.
Wire.write(data.c_str());


7572: <p>I want to write an Arduino program that simply recieves a string (via the I2C wire library) from a master Arduino, then waits for a request, and sends that string back.</p>

<p>Here is my code:</p>

<pre><code>#include &lt;Wire.h&gt;

void setup()
{
  Wire.begin(4); 
  Wire.onReceive(receiveEvent); 
  Wire.onRequest(requestEvent);
}

String data = "";

void loop()
{

}

void receiveEvent(int howMany)
{
  data = "";
  while( Wire.available()){
    data += (char)Wire.read();
  }
}

void requestEvent()
{
    Wire.write(data);
}
</code></pre>

<p>I read in the API that the write() function accepts a string, but I keep getting a "No matching function for call" error.  I tried to simply replace</p>

<pre><code>Wire.write(data);
</code></pre>

<p>with </p>

<pre><code>Wire.write("test");
</code></pre>

<p>and that worked without error.  Why is this the case? </p>
 368: <p><code>data</code> is a <a href="http://arduino.cc/en/Reference/StringObject"><code>String</code></a>. <code>"test"</code> is a <a href="http://arduino.cc/en/Reference/string"><code>char*</code></a>. <a href="http://arduino.cc/en/Reference/WireWrite"><code>Wire.write()</code></a> has no prototype that takes a <code>String</code>.</p>

<pre><code>Wire.write(data.c_str());
</code></pre>
