Note that there are some explanatory texts on larger screens.

plurals
  1. POString reference not updating in function call in C++
    text
    copied!<p>I am writing an arduino library to post http request on web.</p> <p>I am using the String class from <a href="http://arduino.cc/en/Tutorial/TextString" rel="nofollow noreferrer">http://arduino.cc/en/Tutorial/TextString</a></p> <p>My code is behaving strangely when I am referring to my defined string objects after a function call.</p> <p>Here actually I am trying to get the body of my GET request and removing the http headers from the http GET request's response.</p> <p>Following is the description:</p> <p>Method Call: </p> <pre><code> String body; if(pinger.Get(host,path,&amp;body)) { Serial.println("Modified String Outside :"); Serial.println(body); Serial.println(); Serial.println("Modified String Outside Address"); Serial.println((int)&amp;body); } </code></pre> <p>Output</p> <pre><code>Modified String Outside : HTTP/1.1 200 OK Server: Apache-Coyote/1.1 Content-Type: text/html Content-Length: 113 Date: Wed, 13 Jan 2010 14:36:28 GMT &lt;html&gt; &lt;head&gt; &lt;title&gt;Ashish Sharma &lt;/title&gt; &lt;/head&gt; &lt;body&gt; Wed Jan 13 20:06:28 IST 2010 &lt;/body&gt; &lt;/html&gt; Modified String Outside Address 2273 </code></pre> <p>Method Description:</p> <pre><code>bool Pinger::Get(String host, String path, String *response) { bool connection = false; bool status = false; String post1 = "GET "; post1 = post1.append(path); post1 = post1.append(" HTTP/1.1"); String host1 = "Host: "; host1 = host1.append(host); for (int i = 0; i &lt; 10; i++) { if (client.connect()) { client.println(post1); client.println(host1); client.println(); connection = true; break; } } int nlCnt = 0; while (connection) { if (client.available()) { int c = client.read(); response-&gt;append((char) c); if (c == 0x000A &amp;&amp; nlCnt == 0) { nlCnt++; if (response-&gt;contains("200")) { status = true; continue; } else { client.stop(); client.flush(); break; } } } if (!client.connected()) { client.stop(); connection = false; } } response = &amp;response-&gt;substring(response-&gt;indexOf("\n\r\n"),response-&gt;length()); Serial.println("Modified String: "); Serial.println(*response); Serial.println(); Serial.print("Modified String Address: "); Serial.println((int)&amp;response); return status; } </code></pre> <p>Output:</p> <pre><code>Modified String: Ø &lt;html&gt; &lt;head&gt; &lt;title&gt;Ashish Sharma &lt;/title&gt; &lt;/head&gt; &lt;body&gt; Wed Jan 13 20:06:28 IST 2010 &lt;/body&gt; &lt;/html&gt; Modified String Address: 2259 </code></pre> <p>As can be seen from the example the string reference object is giving me the correct string inside the Get method but the reference of the string contents change when the Get method returns.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload