<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[&amp;lt;string&amp;gt; to char[] konvertierung]]></title><description><![CDATA[<p>Sorry, aber die Suchfunktion ist bei mir gerade 1. extrem langsam, 2. habe ich leider nichts passendes finden können.</p>
<p>Die Frage erscheint mir eigentlich zu simpel, aber ich bekomme eine Typenkonvertierung eines STL-strings in ein Char-array nicht gebacken.</p>
<pre><code class="language-cpp">string S=&quot;blablabla&quot;;
char buffer[128];

sprintf(buffer,&quot;%s&quot;, S);       // array bleibt leer
buffer=static_cast&lt;char&gt;(S);   // error: invalid static_cast from...
</code></pre>
<p>Eigenartigerweise akzeptiert eine Funktion, welche einen Zeiger auf ein Array erwartet folgende Referenzübergabe:</p>
<pre><code class="language-cpp">string S=&quot;blablabla&quot;;
function(&amp;S[0]);      // Warum??? vor allem nur mit Index 0
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/224956/lt-string-gt-to-char-konvertierung</link><generator>RSS for Node</generator><lastBuildDate>Thu, 01 Oct 2026 23:16:58 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/224956.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 17 Oct 2008 12:46:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to &amp;lt;string&amp;gt; to char[] konvertierung on Fri, 17 Oct 2008 12:46:43 GMT]]></title><description><![CDATA[<p>Sorry, aber die Suchfunktion ist bei mir gerade 1. extrem langsam, 2. habe ich leider nichts passendes finden können.</p>
<p>Die Frage erscheint mir eigentlich zu simpel, aber ich bekomme eine Typenkonvertierung eines STL-strings in ein Char-array nicht gebacken.</p>
<pre><code class="language-cpp">string S=&quot;blablabla&quot;;
char buffer[128];

sprintf(buffer,&quot;%s&quot;, S);       // array bleibt leer
buffer=static_cast&lt;char&gt;(S);   // error: invalid static_cast from...
</code></pre>
<p>Eigenartigerweise akzeptiert eine Funktion, welche einen Zeiger auf ein Array erwartet folgende Referenzübergabe:</p>
<pre><code class="language-cpp">string S=&quot;blablabla&quot;;
function(&amp;S[0]);      // Warum??? vor allem nur mit Index 0
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1600414</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1600414</guid><dc:creator><![CDATA[LogInNameVergessen]]></dc:creator><pubDate>Fri, 17 Oct 2008 12:46:43 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;lt;string&amp;gt; to char[] konvertierung on Fri, 17 Oct 2008 12:50:38 GMT]]></title><description><![CDATA[<p><code>std::string</code> hat eine Funktion <code>c_str()</code> , die einen <code>const char*</code> zurückgibt. Aber du kannst das dem Array nicht einfach so zuweisen, sondern müsstest evtl. <code>std::copy()</code> verwenden oder eine Schleife schreiben.</p>
<p>P.S.: Du hättest ein bisschen Geduld haben müssen mit der Suche, die Frage taucht oft auf.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1600416</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1600416</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Fri, 17 Oct 2008 12:50:38 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;lt;string&amp;gt; to char[] konvertierung on Fri, 17 Oct 2008 12:51:51 GMT]]></title><description><![CDATA[<p>a) würde gehen mit sprintf(buffer, &quot;%s&quot;, myStr.c_str());<br />
Aber hier darauf achten das buffer groß genug ist.</p>
<p>b) Ist ja auch richtig. Intern hat string auch ein char-array. Und mit &amp;s[0] hast du ein normales c-Array. Im Grunde das gleiche wie c_str(). Außer das es dann const ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1600418</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1600418</guid><dc:creator><![CDATA[Fellhuhn]]></dc:creator><pubDate>Fri, 17 Oct 2008 12:51:51 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;lt;string&amp;gt; to char[] konvertierung on Fri, 17 Oct 2008 12:57:09 GMT]]></title><description><![CDATA[<p>Fellhuhn schrieb:</p>
<blockquote>
<p>b) Ist ja auch richtig. Intern hat string auch ein char-array. Und mit &amp;s[0] hast du ein normales c-Array. Im Grunde das gleiche wie c_str(). Außer das es dann const ist.</p>
</blockquote>
<p>Da wär ich mir jetzt nicht so sicher, ob das bei jeder Implementation so ist. Zumindest würde ich nicht davon ausgehen; ich meinte, <code>std::string</code> wäre im Gegensatz zu <code>std::vector</code> , der intern <strong>immer</strong> ein Array hat, nicht unbedingt C-kompatibel.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1600422</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1600422</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Fri, 17 Oct 2008 12:57:09 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;lt;string&amp;gt; to char[] konvertierung on Fri, 17 Oct 2008 13:06:24 GMT]]></title><description><![CDATA[<p>Danke schön</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1600427</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1600427</guid><dc:creator><![CDATA[LogInNameVergessen]]></dc:creator><pubDate>Fri, 17 Oct 2008 13:06:24 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;lt;string&amp;gt; to char[] konvertierung on Fri, 17 Oct 2008 13:09:23 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>Da wär ich mir jetzt nicht so sicher, ob das bei jeder Implementation so ist. Zumindest würde ich nicht davon ausgehen;</p>
</blockquote>
<p>Das war mein Gedanke, unsicherheit, obwohl es funktioniert. ausserdem sieht &amp;S[0] auf den ersten Blick verwirrend und unleserlich aus</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1600431</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1600431</guid><dc:creator><![CDATA[LogInNameVergessen]]></dc:creator><pubDate>Fri, 17 Oct 2008 13:09:23 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;lt;string&amp;gt; to char[] konvertierung on Fri, 17 Oct 2008 13:10:05 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>Fellhuhn schrieb:</p>
<blockquote>
<p>b) Ist ja auch richtig. Intern hat string auch ein char-array. Und mit &amp;s[0] hast du ein normales c-Array. Im Grunde das gleiche wie c_str(). Außer das es dann const ist.</p>
</blockquote>
<p>Da wär ich mir jetzt nicht so sicher, ob das bei jeder Implementation so ist. Zumindest würde ich nicht davon ausgehen; ich meinte, <code>std::string</code> wäre im Gegensatz zu <code>std::vector</code> , der intern <strong>immer</strong> ein Array hat, nicht unbedingt C-kompatibel.</p>
</blockquote>
<p>Jap. ist auch so. <code>&amp;s[0]</code> von einem <code>std::string s</code> wie einen C-String zu benutzen, produziert undefiniertes Verhalten. s.c_str() muss nicht unbedingt der internen Repräsentation entsprechen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1600433</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1600433</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Fri, 17 Oct 2008 13:10:05 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;lt;string&amp;gt; to char[] konvertierung on Sat, 18 Oct 2008 08:52:23 GMT]]></title><description><![CDATA[<blockquote>
<p>Sorry, aber die Suchfunktion ist bei mir gerade 1. extrem langsam</p>
</blockquote>
<p>Ja. Das nervt mich hier auch. Kleiner Tipp: Versuchs über google. Da findet sich meistens auch was aus diesem Forum. Ist einfacher, als die Suchfunktion hier.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1600699</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1600699</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sat, 18 Oct 2008 08:52:23 GMT</pubDate></item><item><title><![CDATA[Reply to &amp;lt;string&amp;gt; to char[] konvertierung on Sat, 18 Oct 2008 09:28:54 GMT]]></title><description><![CDATA[<p>zu a) sprintf ist eine C-Funktion und hat aus verschiedenen Gründen in C++ nichts zu suchen (Längenchecks, Typsicherheit etc.)</p>
<p>Wenn du den char-array readonly benutzen möchtest reicht ein einfaches</p>
<pre><code class="language-cpp">string S = &quot;blablabla&quot;;
function(S.c_str());
</code></pre>
<p>(wurde ja auch schon gesagt)</p>
<p>Wenn du es beschreibbar haben möchtest kommst du um eine Kopie nicht herum, allerdings dann bitte sicher (und auch wieder mit C++-Funktionen):</p>
<pre><code class="language-cpp">std::string S = &quot;blablabla&quot;;
static std::size_t const buflen = 1024;
char buf[buflen] = {0};
std::copy(S.c_str(), S.c_str() + std::max(S.length(), buflen), buf);

function(buf);
</code></pre>
<p>alternativ mit einem dynamischen Array, damit die Länge nicht auf einen festen Wert beschränkt ist:</p>
<pre><code class="language-cpp">std::string S = &quot;blablabla&quot;;
std::size_t buflen = S.length();
char* buf = new char[buflen];
std::copy(S.c_str(), S.c_str()+buflen, buf);

function(buf);
//...
delete buf; //nicht vergessen...
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1600717</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1600717</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Sat, 18 Oct 2008 09:28:54 GMT</pubDate></item></channel></rss>