<?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[const char* in char* umwandeln]]></title><description><![CDATA[<p>Kann ich folgende Umwandlung</p>
<pre><code class="language-cpp">std::string CommandTriggerContinous;
CommandTriggerContinous = &quot;init&quot;;

char * buffer = const_cast&lt;char*&gt;(CommandTriggerContinous.c_str());

GPIB_Out(buffer);
</code></pre>
<p>gefahrenlos durchführen wenn ich nicht weiß ob 'buffer' später noch geändert wird?</p>
<p>Konkret übergebe ich 'buffer' an eine Klasse zur Geräteansteuerung und weiß nicht was damit passiert.</p>
<p>Matthias</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/210372/const-char-in-char-umwandeln</link><generator>RSS for Node</generator><lastBuildDate>Tue, 13 Oct 2026 11:38:15 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/210372.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 10 Apr 2008 10:51:46 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to const char* in char* umwandeln on Thu, 10 Apr 2008 10:51:46 GMT]]></title><description><![CDATA[<p>Kann ich folgende Umwandlung</p>
<pre><code class="language-cpp">std::string CommandTriggerContinous;
CommandTriggerContinous = &quot;init&quot;;

char * buffer = const_cast&lt;char*&gt;(CommandTriggerContinous.c_str());

GPIB_Out(buffer);
</code></pre>
<p>gefahrenlos durchführen wenn ich nicht weiß ob 'buffer' später noch geändert wird?</p>
<p>Konkret übergebe ich 'buffer' an eine Klasse zur Geräteansteuerung und weiß nicht was damit passiert.</p>
<p>Matthias</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1489606</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1489606</guid><dc:creator><![CDATA[pospiech]]></dc:creator><pubDate>Thu, 10 Apr 2008 10:51:46 GMT</pubDate></item><item><title><![CDATA[Reply to const char* in char* umwandeln on Thu, 10 Apr 2008 10:54:31 GMT]]></title><description><![CDATA[<p>Nein.<br />
Mach eine Kopie und übergib diese.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1489608</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1489608</guid><dc:creator><![CDATA[simon.gysi]]></dc:creator><pubDate>Thu, 10 Apr 2008 10:54:31 GMT</pubDate></item><item><title><![CDATA[Reply to const char* in char* umwandeln on Thu, 10 Apr 2008 10:55:28 GMT]]></title><description><![CDATA[<p>simon.gysi schrieb:</p>
<blockquote>
<p>Nein.<br />
Mach eine Kopie und übergib diese.</p>
</blockquote>
<p>Wie mache ich denn eine Kopie? Das wollte ich ja gerade mit obigem Code erreichen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1489611</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1489611</guid><dc:creator><![CDATA[pospiech]]></dc:creator><pubDate>Thu, 10 Apr 2008 10:55:28 GMT</pubDate></item><item><title><![CDATA[Reply to const char* in char* umwandeln on Thu, 10 Apr 2008 10:56:16 GMT]]></title><description><![CDATA[<p>pospiech schrieb:</p>
<blockquote>
<p>Kann ich folgende Umwandlung...gefahrenlos durchführen wenn ich nicht weiß ob 'buffer' später noch geändert wird?</p>
</blockquote>
<p>Nein. Dann solltest du lieber den Inhalt des Strings in ein separates char[] umkopieren. const_cast sollte nur verwendet werden wenn definitiv keine Datenänderung durchgeführt wird.</p>
<p>cu André</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1489612</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1489612</guid><dc:creator><![CDATA[asc]]></dc:creator><pubDate>Thu, 10 Apr 2008 10:56:16 GMT</pubDate></item><item><title><![CDATA[Reply to const char* in char* umwandeln on Thu, 10 Apr 2008 11:10:17 GMT]]></title><description><![CDATA[<p>asc schrieb:</p>
<blockquote>
<p>pospiech schrieb:</p>
<blockquote>
<p>Kann ich folgende Umwandlung...gefahrenlos durchführen wenn ich nicht weiß ob 'buffer' später noch geändert wird?</p>
</blockquote>
<p>Nein. Dann solltest du lieber den Inhalt des Strings in ein separates char[] umkopieren.</p>
</blockquote>
<p>Wie soll das dann gehen?</p>
<p>folgendes ist nämlich nicht möglich</p>
<pre><code class="language-cpp">char [] buffer = CommandTriggerContinous.c_str();
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1489627</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1489627</guid><dc:creator><![CDATA[pospiech]]></dc:creator><pubDate>Thu, 10 Apr 2008 11:10:17 GMT</pubDate></item><item><title><![CDATA[Reply to const char* in char* umwandeln on Thu, 10 Apr 2008 11:13:53 GMT]]></title><description><![CDATA[<p>Entweder in einer Schleife oder strcpy oder memcpy.<br />
Oder std::copy verwenden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1489629</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1489629</guid><dc:creator><![CDATA[simon.gysi]]></dc:creator><pubDate>Thu, 10 Apr 2008 11:13:53 GMT</pubDate></item><item><title><![CDATA[Reply to const char* in char* umwandeln on Thu, 10 Apr 2008 11:19:50 GMT]]></title><description><![CDATA[<p>simon.gysi schrieb:</p>
<blockquote>
<p>Entweder in einer Schleife oder strcpy oder memcpy.<br />
Oder std::copy verwenden.</p>
</blockquote>
<p>strcpy geht nicht:</p>
<pre><code class="language-cpp">char buffer[100];
	strcpy(buffer,CommandTriggerContinous.c_str());
	GPIB_Out(&amp;buffer);
</code></pre>
<blockquote>
<p>Konvertierung des Parameters 1 von 'char (*)[100]' in 'char *' nicht möglich</p>
</blockquote>
<p>Da es sich hier ja eigentlich um ein übliches Problem handelt müsste es doch auch eine grundsätzlich funktionierende Lösung geben? Mir wäre sehr geholfe, wenn jemand dazu ein Code Beispiel posten könnte.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1489638</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1489638</guid><dc:creator><![CDATA[pospiech]]></dc:creator><pubDate>Thu, 10 Apr 2008 11:19:50 GMT</pubDate></item><item><title><![CDATA[Reply to const char* in char* umwandeln on Thu, 10 Apr 2008 11:23:31 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">char* buffer = new char[CommandTriggerContinous.size()+1];
strcpy(buffer,CommandTriggerContinous.c_str());
GPIB_Out(&amp;buffer);
delete[] buffer;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1489642</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1489642</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Thu, 10 Apr 2008 11:23:31 GMT</pubDate></item><item><title><![CDATA[Reply to const char* in char* umwandeln on Thu, 10 Apr 2008 11:33:19 GMT]]></title><description><![CDATA[<p>pospiech schrieb:</p>
<blockquote>
<p>simon.gysi schrieb:</p>
<blockquote>
<p>Nein.<br />
Mach eine Kopie und übergib diese.</p>
</blockquote>
<p>Wie mache ich denn eine Kopie? Das wollte ich ja gerade mit obigem Code erreichen.</p>
</blockquote>
<p>Nunja, dass const_<strong>cast</strong> einen <strong>cast</strong> durchführt und keine Kopie, ist nicht soooo schwer zu erraten. <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>pospiech schrieb:</p>
<blockquote>
<p>....<br />
strcpy geht nicht:</p>
<pre><code class="language-cpp">char buffer[100];
	strcpy(buffer,CommandTriggerContinous.c_str());
	GPIB_Out(&amp;buffer);
</code></pre>
<blockquote>
<p>Konvertierung des Parameters 1 von 'char (*)[100]' in 'char *' nicht möglich</p>
</blockquote>
<p>....</p>
</blockquote>
<p>doch das &quot;geht&quot; ... aber Du solltest Dir angewöhnen, auf die Zeilenangaben bei den Fehlermeldungen zu achten, denn dass</p>
<p>pospiech schrieb:</p>
<blockquote>
<p>....</p>
<pre><code class="language-cpp">GPIB_Out( &amp;  buffer); // &lt;- &quot;&amp;&quot; hat da nichts zu suchen
</code></pre>
</blockquote>
<p>nicht geht, ist relativ klar (und genau das, was der Compiler anmeckern dürfte).</p>
<p>Übrigens: NIEMALS !!! ungeprüfte Längen bei str_/_printf//...-Funktionen verwenden !!</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1489647</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1489647</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Thu, 10 Apr 2008 11:33:19 GMT</pubDate></item><item><title><![CDATA[Reply to const char* in char* umwandeln on Thu, 10 Apr 2008 11:33:10 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">std::string s = &quot;test&quot;;

char buf1[10] = { '\0' };
strcpy(buf1, s.c_str());

char buf2[10] = { '\0' };
memcpy(buf2, s.data(), s.size());

char buf3[10] = { '\0' };
size_t len = s.copy(buf3, s.size());

char buf4[10] = { '\0' };
std::copy(s.begin(), s.end(), buf4);
</code></pre>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/14053">@pospiech</a>:<br />
Die Variante mit str<strong>n</strong>cpy kannst du dir ja selbst schreiben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1489652</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1489652</guid><dc:creator><![CDATA[simon.gysi]]></dc:creator><pubDate>Thu, 10 Apr 2008 11:33:10 GMT</pubDate></item><item><title><![CDATA[Reply to const char* in char* umwandeln on Thu, 10 Apr 2008 14:15:44 GMT]]></title><description><![CDATA[<p>brrr, immer diese char-arrays.<br />
so geht's auch:</p>
<pre><code class="language-cpp">std::string x = &quot;init&quot;;

vector&lt;char&gt; buffer(x.begin(), x.end());

GPIB_Out(&amp;buffer[0]);
</code></pre>
<p>und du musst dich nirgends selbst ums aufräumen kümmern.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1489799</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1489799</guid><dc:creator><![CDATA[queer_boy]]></dc:creator><pubDate>Thu, 10 Apr 2008 14:15:44 GMT</pubDate></item></channel></rss>