<?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[string nach c array]]></title><description><![CDATA[<p>Mahlzeit,</p>
<p>klappt dieser Code hier immer?</p>
<pre><code class="language-cpp">char str[64];
std::string otherStr = ...
strncpy(str, otherStr.c_str(), 63);
</code></pre>
<p>Oder koennte das mal schief gehen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/288688/string-nach-c-array</link><generator>RSS for Node</generator><lastBuildDate>Wed, 19 Aug 2026 19:36:06 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/288688.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 21 Jun 2011 08:55:03 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 08:55:03 GMT]]></title><description><![CDATA[<p>Mahlzeit,</p>
<p>klappt dieser Code hier immer?</p>
<pre><code class="language-cpp">char str[64];
std::string otherStr = ...
strncpy(str, otherStr.c_str(), 63);
</code></pre>
<p>Oder koennte das mal schief gehen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081510</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081510</guid><dc:creator><![CDATA[konvertierer()]]></dc:creator><pubDate>Tue, 21 Jun 2011 08:55:03 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 08:57:10 GMT]]></title><description><![CDATA[<p>eigentlich sollte nichts passieren, also nicht böses.<br />
du überschreitest die arraygrenzen nicht und du hast speicher angefordert...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081511</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081511</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Tue, 21 Jun 2011 08:57:10 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 09:02:00 GMT]]></title><description><![CDATA[<p>Ich frage vor allem, weil ich nicht weiss ob std::string den C String immer mit einer 0 terminiert. Wenn das nicht der Fall waere, waere str ja auch nicht 0 terminiert und wurde dann spaeter bei Benutzung von string funktionen wie strcmp() explodieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081512</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081512</guid><dc:creator><![CDATA[konvertierer()]]></dc:creator><pubDate>Tue, 21 Jun 2011 09:02:00 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 09:06:34 GMT]]></title><description><![CDATA[<p>Natürlich ist der 0-terminiert, ergäbe anders keinen Sinn.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081513</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081513</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Tue, 21 Jun 2011 09:06:34 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 09:06:41 GMT]]></title><description><![CDATA[<p>Die Rückgabe von c_str() ist immer nullterminiert. Allerdings ist Dein Zielstring nicht nullterminiert, wenn otherStr länger als 63 Zeichen ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081514</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081514</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Tue, 21 Jun 2011 09:06:41 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 09:08:44 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>Natürlich ist der 0-terminiert, ergäbe anders keinen Sinn.</p>
</blockquote>
<p>Wieso sollte das keinen Sinn ergeben? Man koennte std::string absolut ohne 0 Terminierer implementieren.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081515</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081515</guid><dc:creator><![CDATA[konvertierer()]]></dc:creator><pubDate>Tue, 21 Jun 2011 09:08:44 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 09:11:04 GMT]]></title><description><![CDATA[<p>LordJaxom schrieb:</p>
<blockquote>
<p>Die Rückgabe von c_str() ist immer nullterminiert. Allerdings ist Dein Zielstring nicht nullterminiert, wenn otherStr länger als 63 Zeichen ist.</p>
</blockquote>
<p>Also am besten so machen, um 100%ig sicher zu gehen?</p>
<pre><code class="language-cpp">strncpy(str, otherStr.c_str(), 63);
str[63] = 0;
</code></pre>
<p>?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081517</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081517</guid><dc:creator><![CDATA[konvertierer()]]></dc:creator><pubDate>Tue, 21 Jun 2011 09:11:04 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 09:12:27 GMT]]></title><description><![CDATA[<p>konvertierer() schrieb:</p>
<blockquote>
<p>314159265358979 schrieb:</p>
<blockquote>
<p>Natürlich ist der 0-terminiert, ergäbe anders keinen Sinn.</p>
</blockquote>
<p>Wieso sollte das keinen Sinn ergeben? Man koennte std::string absolut ohne 0 Terminierer implementieren.</p>
</blockquote>
<p>Ist richtig, aber das Ergebnis von c_str ist wie gesagt garantiert terminiert. Um dein Vorhaben sicherer zu machen, würde ich einen vector benutzen. Oder du musst bei deiner Lösung halt immer das letzte Zeichen mit 0 überschreiben.</p>
<pre><code class="language-cpp">void C(char* str)
{
    str[0] = 'x';
}

int main()
{
    std::string s = &quot;hallo&quot;;

    std::vector&lt;char&gt; cstr(s.begin(), s.end());
    cstr.push_back('\0');
    C(&amp;cstr[0]);

    s.assign(cstr.begin(), cstr.end());
    std::cout &lt;&lt; s;

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2081518</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081518</guid><dc:creator><![CDATA[brotbernd]]></dc:creator><pubDate>Tue, 21 Jun 2011 09:12:27 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 09:20:48 GMT]]></title><description><![CDATA[<p>brotbernd schrieb:</p>
<blockquote>
<p>Um dein Vorhaben sicherer zu machen, würde ich einen vector benutzen.</p>
</blockquote>
<p>Warum so kompliziert und nicht einfach str.substr(0, 63).c_str() ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081520</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081520</guid><dc:creator><![CDATA[GPC]]></dc:creator><pubDate>Tue, 21 Jun 2011 09:20:48 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 09:21:13 GMT]]></title><description><![CDATA[<p>konvertierer() schrieb:</p>
<blockquote>
<p>Ich frage vor allem, weil ich nicht weiss ob std::string den C String immer mit einer 0 terminiert.</p>
</blockquote>
<p>Dann schaut man am besten mal nach <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /> <a href="http://www.cplusplus.com/reference/string/string/c_str/" rel="nofollow">string::c_str</a></p>
<p>314159265358979 schrieb:</p>
<blockquote>
<p>Natürlich ist der 0-terminiert, ergäbe anders keinen Sinn.</p>
</blockquote>
<p>Dass es garantiert ist, dass er 0-terminiert ist haben wir nun festgestellt. Was das allerdings mit Sinn zu tun hat ist mir schleierhaft. Gibt schliesslich auch data() (nicht 0-terminiert).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081521</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081521</guid><dc:creator><![CDATA[notLoggedIn]]></dc:creator><pubDate>Tue, 21 Jun 2011 09:21:13 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 09:51:02 GMT]]></title><description><![CDATA[<p>Es macht Sinn, sich an C-Konventionen zu halten. Mein Gott, ist das so schwer?<br />
.data() gibt ja auch keinen String zurück. &lt;.&lt;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081533</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081533</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Tue, 21 Jun 2011 09:51:02 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 09:52:41 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>Es macht Sinn, sich an C-Konventionen zu halten. Mein Gott, ist das so schwer?<br />
.data() gibt ja auch keinen String zurück. &lt;.&lt;</p>
</blockquote>
<p>Wieso sollte sich eine C++ Klasse zwingend an C Konventionen halten?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081535</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081535</guid><dc:creator><![CDATA[nenene--]]></dc:creator><pubDate>Tue, 21 Jun 2011 09:52:41 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 09:54:58 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>Es macht Sinn, sich an C-Konventionen zu halten. Mein Gott, ist das so schwer?<br />
.data() gibt ja auch keinen String zurück. &lt;.&lt;</p>
</blockquote>
<p>Der Rückgabewert von data() und c_str() ist absolut identisch, nämlich: const char *</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081536</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081536</guid><dc:creator><![CDATA[notLoggedIn]]></dc:creator><pubDate>Tue, 21 Jun 2011 09:54:58 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 10:02:24 GMT]]></title><description><![CDATA[<p>Weil es dumm wäre, eine Inkompatibilität mit C-Funktionen künstlich herzustellen.</p>
<p>Falsch, sie haben den selben Rückgabetyp, aber .data() liefert einen Zeiger auf Daten, .c_str() einen String.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081537</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081537</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Tue, 21 Jun 2011 10:02:24 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 10:09:43 GMT]]></title><description><![CDATA[<p>notLoggedIn schrieb:</p>
<blockquote>
<p>Der Rückgabewert von data() und c_str() ist absolut identisch, nämlich: const char *</p>
</blockquote>
<p>const char* ist der <strong>Typ</strong>, nicht der Wert. Der Wert kann durchaus unterschiedlich sein. data() muss nämlich nicht nullterminiert sein.</p>
<p>konvertierer() schrieb:</p>
<blockquote>
<p>Wieso sollte das keinen Sinn ergeben? Man koennte std::string absolut ohne 0 Terminierer implementieren.</p>
</blockquote>
<p>Kann man, tut man vermutlich auch <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>314159265358979 schrieb:</p>
<blockquote>
<p>Natürlich ist der 0-terminiert, ergäbe anders keinen Sinn.</p>
</blockquote>
<p>So ohne Begründung ist das garnicht so &quot;natürlich&quot;. Das ist es erst, wenn man dazu sagt, dass c_str() eben genau zu dem Zweck existiert, um die Umwandlung in C-Strings zu ermöglichen - was ohne den Nullterminierer tatsächlich sinnfrei wäre.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081541</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081541</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Tue, 21 Jun 2011 10:09:43 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 10:13:16 GMT]]></title><description><![CDATA[<p>nenene-- schrieb:</p>
<blockquote>
<p>Wieso sollte sich eine C++ Klasse zwingend an C Konventionen halten?</p>
</blockquote>
<p>Weil die Funktion <code>c_str</code> (C-String) heißt und hier explizit bereits im Namen gesagt wird, dass es ein C-Style String ist, eine Funktion, die <em>extra dafür eingebaut wurde</em>, um C-kompatibel zu sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081542</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081542</guid><dc:creator><![CDATA[_roll_]]></dc:creator><pubDate>Tue, 21 Jun 2011 10:13:16 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 10:23:33 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>konvertierer() schrieb:</p>
<blockquote>
<p>Wieso sollte das keinen Sinn ergeben? Man koennte std::string absolut ohne 0 Terminierer implementieren.</p>
</blockquote>
<p>Kann man, tut man vermutlich auch <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
</blockquote>
<p>Allgemeine, ungewertete Info: Die String-Implementierung beim g++ ist immer nullterminiert. Ein Kommentar im Code sagt, dass der C++-Standard 21.3.4 dies erzwingt. Ich kann mich dem zwar nicht ganz anschließen, aber ich stimme zu, dass ein Programmierer wirklich absichtlich böse sein müsste, um dies anders zu implementieren. Es wäre nämlich nötig bei jedem Elementzugriff, eine Fallunterscheidung zu machen, die man sich ansonsten spart. (Falls man ohnehin einen Zugriff mit Test auf Überschreitung der Grenzen hat ist das hingegen geschenkt).</p>
<p>P.S.: 21.3.4 sagt</p>
<blockquote>
<p><strong>21.3.4: basic_string element access</strong></p>
<p>`const_reference operator []( size_type pos ) const ;</p>
<p>reference operator []( size_type pos );`</p>
<p>Returns: If <code>pos &lt; size(), returns *(begin() + pos )</code> . Otherwise, if <code>pos == size()</code> , the const version returns <code>charT()</code> . Otherwise, the behavior is undefined.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2081544</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081544</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 21 Jun 2011 10:23:33 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 10:43:17 GMT]]></title><description><![CDATA[<p>Interessant.</p>
<p>Zitat aus &quot;Effective STL&quot; von Scott Meyers:</p>
<blockquote>
<p>...The approach to getting a pointer to container data that works for vectors isn't reliable for strings, because (1) <strong>the data for strings are not guaranteed to be stored in contiguous memory</strong>, and (2) <strong>the internal representation of a string is not guaranteed to end with a null character</strong>. This explains the existence of the string member function c_str, which returns a pointer to the value of the string in a form designed for C. We can thus pass a string s to this function....</p>
</blockquote>
<p>Was ja im Widerspruch zum Kommentar mit Bezug auf 21.3.4 steht.<br />
21.4.1.5 in <a href="http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2011/n3242.pdf" rel="nofollow">N3242</a> sagt zwar, dass ein zusammenhängender Speicherbereich genutzt werden <strong>sollte</strong>, aber bezüglich der 0-terminierung habe ich nichts gefunden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081553</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081553</guid><dc:creator><![CDATA[notLoggedIn]]></dc:creator><pubDate>Tue, 21 Jun 2011 10:43:17 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 11:29:09 GMT]]></title><description><![CDATA[<p>Ich seh da gar keinen Widerspruch.</p>
<blockquote>
<p>returns *(begin() + pos )</p>
</blockquote>
<p>Also Iterator auf den Anfang um pos erhöht und dereferenziert. Wie das implementiert steht da ja nicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081577</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081577</guid><dc:creator><![CDATA[brotbernd]]></dc:creator><pubDate>Tue, 21 Jun 2011 11:29:09 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 11:35:20 GMT]]></title><description><![CDATA[<p>brotbernd schrieb:</p>
<blockquote>
<p>Ich seh da gar keinen Widerspruch.</p>
<blockquote>
<p>returns *(begin() + pos )</p>
</blockquote>
<p>Also Iterator auf den Anfang um pos erhöht und dereferenziert. Wie das implementiert steht da ja nicht.</p>
</blockquote>
<p>Es geht glaube ich eher um die Klausel, dass bei pos == size() der Wert charT() zurückgegeben wird. Wenn du nicht bei jedem Zugriff da drauf prüfen willst, dann sollte da besser gleich der passende Wert stehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081581</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081581</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 21 Jun 2011 11:35:20 GMT</pubDate></item><item><title><![CDATA[Reply to string nach c array on Tue, 21 Jun 2011 11:50:03 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Es geht glaube ich eher um die Klausel, dass bei pos == size() der Wert charT() zurückgegeben wird. Wenn du nicht bei jedem Zugriff da drauf prüfen willst, dann sollte da besser gleich der passende Wert stehen.</p>
</blockquote>
<p>Dabei stellt sich dann natürlich die Frage, wo &quot;da&quot; dann ist. Wenn man den Speicher im vector-style am Stück irgendwo liegen hat ist das klar, aber wenns eben nicht am Stück ist heißt der Satz nicht mehr als &quot;Dereferenzierung des end()-Iterators liefert CharT()&quot;, da begin()+size() == end()</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2081589</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2081589</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Tue, 21 Jun 2011 11:50:03 GMT</pubDate></item></channel></rss>