<?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[Problem c-string]]></title><description><![CDATA[<p>Hallo!<br />
Leider habe ich überhaupt keine Ahnung und Erfahrung mit c. Der folgende Beispielcode beschreibt, denke ich, mein Problem ganz gut.</p>
<pre><code class="language-cpp">#include &lt;cstring&gt;

class MyClass
{
    private:
       char* name;

    public:
       MyClass(const char* c)
       {
          // Wie jetzt speicher reservieren und name c zuweisen?
          // strncpy(name, c, ???)?
       }
}
</code></pre>
<p>Oder wäre es sinnvoll c++ Strings zu verwenden? Einerseits kann ich so noch etwas lernen...</p>
<p>Danke und Gruß, Vanish</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/221338/problem-c-string</link><generator>RSS for Node</generator><lastBuildDate>Tue, 06 Oct 2026 03:21:58 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/221338.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 27 Aug 2008 22:01:40 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem c-string on Wed, 27 Aug 2008 22:01:40 GMT]]></title><description><![CDATA[<p>Hallo!<br />
Leider habe ich überhaupt keine Ahnung und Erfahrung mit c. Der folgende Beispielcode beschreibt, denke ich, mein Problem ganz gut.</p>
<pre><code class="language-cpp">#include &lt;cstring&gt;

class MyClass
{
    private:
       char* name;

    public:
       MyClass(const char* c)
       {
          // Wie jetzt speicher reservieren und name c zuweisen?
          // strncpy(name, c, ???)?
       }
}
</code></pre>
<p>Oder wäre es sinnvoll c++ Strings zu verwenden? Einerseits kann ich so noch etwas lernen...</p>
<p>Danke und Gruß, Vanish</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1572600</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1572600</guid><dc:creator><![CDATA[VanishOxiAction]]></dc:creator><pubDate>Wed, 27 Aug 2008 22:01:40 GMT</pubDate></item><item><title><![CDATA[Reply to Problem c-string on Wed, 27 Aug 2008 22:24:19 GMT]]></title><description><![CDATA[<p>Hi</p>
<pre><code class="language-cpp">#include &lt;string&gt;

class MyClass
{
    private:
       std::string name;

    public:
       MyClass(const char* c) : name(c) { }
}
</code></pre>
<p>benutze immer std::string in C++ und nicht die C-Strings</p>
<p>Mfg</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1572612</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1572612</guid><dc:creator><![CDATA[std__string]]></dc:creator><pubDate>Wed, 27 Aug 2008 22:24:19 GMT</pubDate></item><item><title><![CDATA[Reply to Problem c-string on Wed, 27 Aug 2008 22:25:00 GMT]]></title><description><![CDATA[<p>Das Problem ist halt, dass man mit C-Strings (also <code>char</code> -Arrays) genau aufpassen muss, was man tut. Man schreibt damit in ungültige Speicherbereiche, ehe man sich versieht.</p>
<p>Bei deinem Beispiel müsstest du zuerst mit <code>new</code> Speicher für <code>name</code> anfordern und dann diesen Speicher mit <code>strcpy()</code> oder <code>strncpy()</code> füllen. Genauere Beschreibungen zu diesen Funktionen erhältst du auf <a href="http://www.cplusplus.com" rel="nofollow">www.cplusplus.com</a> - eine ziemlich gute Referenz für C++. Im Destruktor müsstest du dann mit <code>delete</code> wieder den Speicher freigeben. Eventuell müsstest du auch noch den Kopierkonstruktor und Zuweisungsoperator anpassen - also unterschätz den Aufwand mit <code>char*</code> nicht <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>Aber wie du gesagt hast, die C-Strings sollte man besser nur als Übung verwenden. In C++ gibt es die Klasse <code>std::string</code> , die man viel einfacher handhaben kann (z.B. durch Zuweisungsoperator). Diese würde ich auch in Zukunft nutzen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1572613</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1572613</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Wed, 27 Aug 2008 22:25:00 GMT</pubDate></item><item><title><![CDATA[Reply to Problem c-string on Thu, 28 Aug 2008 04:35:09 GMT]]></title><description><![CDATA[<p>Man kann auch std::string wie ein char-array verwenden...</p>
<pre><code class="language-cpp">std::string myString;
strcpy(&amp;myString[0], &quot;Hello World&quot;);
</code></pre>
<p>lg</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1572646</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1572646</guid><dc:creator><![CDATA[string-fan]]></dc:creator><pubDate>Thu, 28 Aug 2008 04:35:09 GMT</pubDate></item><item><title><![CDATA[Reply to Problem c-string on Thu, 28 Aug 2008 04:45:42 GMT]]></title><description><![CDATA[<p>string-fan schrieb:</p>
<blockquote>
<p>Man kann auch std::string wie ein char-array verwenden...</p>
</blockquote>
<p>Nein, kann man nicht. Auch dann nicht, wenn man vorher sicherstellt, dass der Inhalt des Strings lang genug ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1572647</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1572647</guid><dc:creator><![CDATA[MFK]]></dc:creator><pubDate>Thu, 28 Aug 2008 04:45:42 GMT</pubDate></item><item><title><![CDATA[Reply to Problem c-string on Thu, 28 Aug 2008 06:41:41 GMT]]></title><description><![CDATA[<p>MFK schrieb:</p>
<blockquote>
<p>string-fan schrieb:</p>
<blockquote>
<p>Man kann auch std::string wie ein char-array verwenden...</p>
</blockquote>
<p>Nein, kann man nicht. Auch dann nicht, wenn man vorher sicherstellt, dass der Inhalt des Strings lang genug ist.</p>
</blockquote>
<p>Naja ... &quot;können&quot; kann man schon. Aber das Ergebnis sollte einem egal sein ... <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>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1572672</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1572672</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Thu, 28 Aug 2008 06:41:41 GMT</pubDate></item><item><title><![CDATA[Reply to Problem c-string on Thu, 28 Aug 2008 06:58:45 GMT]]></title><description><![CDATA[<p>string-fan schrieb:</p>
<blockquote>
<p>Man kann auch std::string wie ein char-array verwenden...</p>
<pre><code class="language-cpp">std::string myString;
strcpy(&amp;myString[0], &quot;Hello World&quot;);
</code></pre>
<p>lg</p>
</blockquote>
<p>Sowas in einem Thread zu posten, wo Anfängerfragen besprochen werden, ist gemein. Diejenigen könnten nämlich auf die Idee kommen, solchen bullshit tatsächlich zu verwenden...</p>
<p>Da fehlt n großes Schild dran:<br />
<strong>Don't try this at home!</strong></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1572684</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1572684</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Thu, 28 Aug 2008 06:58:45 GMT</pubDate></item><item><title><![CDATA[Reply to Problem c-string on Thu, 28 Aug 2008 19:06:14 GMT]]></title><description><![CDATA[<p>Danke für die Antworten! Etwas spät, war aber viel beschäftigt.</p>
<p>Dachte die ganze Zeit, ich muss irgendwie mit malloc speicher anfordern. Aber auf new bin ich nicht gekommen, aber klar, ist ja c++. Manchmal wäre es wohl sinnvoller gewesen, erst c zu lernen.</p>
<p>Zurück zu meiner Frage.</p>
<pre><code class="language-cpp">#include &lt;cstring&gt;

class MyClass
{
    private:
       char* name[];

    public:
       MyClass(const char* c)
       {
          name = new char[strlen(c) + 1];
          strncpy(name, c, strlen(c) + 1)
       }
}
</code></pre>
<p>Verbesserungsvorschläge? (werde schon c++ strings verwenden, bin nur etwas neugierig)</p>
<p>Ansonsten vielen Dank für die tolle Hilfe,<br />
Gruß Vanish</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1573231</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1573231</guid><dc:creator><![CDATA[VanishOxiAction]]></dc:creator><pubDate>Thu, 28 Aug 2008 19:06:14 GMT</pubDate></item><item><title><![CDATA[Reply to Problem c-string on Thu, 28 Aug 2008 19:13:35 GMT]]></title><description><![CDATA[<blockquote>
<p>Verbesserungsvorschläge? (werde schon c++ strings verwenden, bin nur etwas neugierig)</p>
</blockquote>
<p>- Wo gibst du das ganze wieder frei?<br />
- Was passiert, wenn du MyClass kopierst?<br />
- Was passiert, wenn du MyClass einem anderen Objekt zuweist?<br />
- Was passiert, wenn new bad_alloc wirft?</p>
<p>Da kann also noch recht viel schiefgehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1573237</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1573237</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Thu, 28 Aug 2008 19:13:35 GMT</pubDate></item><item><title><![CDATA[Reply to Problem c-string on Thu, 28 Aug 2008 19:19:14 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>string-fan schrieb:</p>
<blockquote>
<p>Man kann auch std::string wie ein char-array verwenden...</p>
<pre><code class="language-cpp">std::string myString;
strcpy(&amp;myString[0], &quot;Hello World&quot;);
</code></pre>
<p>lg</p>
</blockquote>
<p>Sowas in einem Thread zu posten, wo Anfängerfragen besprochen werden, ist gemein. Diejenigen könnten nämlich auf die Idee kommen, solchen bullshit tatsächlich zu verwenden...</p>
<p>Da fehlt n großes Schild dran:<br />
<strong>Don't try this at home!</strong></p>
</blockquote>
<p>Das darf man und ist auch absolut legal. std::string und std::vector sind nämlich &quot;c-kompatibel&quot;. Da std::string die Stringlänge nicht an einer terminierenden 0 festmacht stört eine 0 mitten im String auch nicht.<br />
Die Ausgabe auf einen std::ostream ist auch kein Problem.</p>
<p>Ich sage nicht, dass diese Art der Stringmanipulation gut ist, aber falsches korrigiere ich halt <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="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1573241</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1573241</guid><dc:creator><![CDATA[Tippgeber]]></dc:creator><pubDate>Thu, 28 Aug 2008 19:19:14 GMT</pubDate></item><item><title><![CDATA[Reply to Problem c-string on Thu, 28 Aug 2008 19:27:32 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">class MyClass
{
private:
	char* name;

public:
	explicit MyClass( const char* c = &quot;&quot; ) 
	try 
	: name( new char[ std::char_traits&lt; char &gt;::length( c )+1 ] )
	{
	  Initialize( c );
	}
	catch ( ... )
	{
	  std::cerr &lt;&lt; &quot;MyClass::c'tor failed&quot;;
	  throw;
	}

	MyClass( const MyClass&amp; other )
	try 
	: name( new char[ std::char_traits&lt; char &gt;::length( other.name )+1 ] )
	{
		Initialize( other.name );
	}
	catch ( ... )
	{
	  std::cerr &lt;&lt; &quot;MyClass::copy c'tor failed&quot;;
	  throw;
	}

	MyClass&amp; operator=( const MyClass&amp; other ) 
	{
		MyClass( other ).Swap( *this );
		return *this;
	}

	~MyClass()
	{
	  delete [] name;
	}

	const char* Name() const { return name; }

private:
	void Swap( MyClass&amp; other )
	{
		std::swap( name, other.name );
	}

	void Initialize( const char* str )
	{
	  std::char_traits&lt; char &gt;::copy(
		  name, str, std::char_traits&lt; char &gt;::length( str )+1 );
	}
};
</code></pre>
<p><img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f4a1.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--light_bulb"
      title=":bulb:"
      alt="💡"
    /> :p</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1573246</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1573246</guid><dc:creator><![CDATA[David_pb]]></dc:creator><pubDate>Thu, 28 Aug 2008 19:27:32 GMT</pubDate></item><item><title><![CDATA[Reply to Problem c-string on Thu, 28 Aug 2008 19:40:11 GMT]]></title><description><![CDATA[<p>David_pb schrieb:</p>
<blockquote>
<p>...</p>
</blockquote>
<p>LOL<br />
Ich habe mir noch gedacht, ob ich dein Beispiel aus <a href="http://sp.de" rel="nofollow">sp.de</a> posten soll, aber dann habe ich gedacht, dass er besser zuerst selber überlegen sollte. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1573249</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1573249</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Thu, 28 Aug 2008 19:40:11 GMT</pubDate></item></channel></rss>