<?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[Array mit Strings ----PROBLEM---]]></title><description><![CDATA[<p>Hallo Forum,</p>
<p>ich möchte ein Array der Größe 3 mit 3 strings füllen, die ich dann nachher einzeln abfragen kann.</p>
<p>Wie mache ich das am besten.</p>
<p>Bisher habe ich eine Methode fillArray:</p>
<pre><code class="language-cpp">void fillArray () 
{ char* strings [2];
  char* name;
  int n;
  for (n=0; n&lt;2; n++) {
     cin &gt;&gt; name;
     strings[n]= name;
  }
}
</code></pre>
<p>Und wie steuere ich dann nachher die einzelnen Einträge an?</p>
<p>Vielen Dank<br />
Liebe Grüße<br />
Tina</p>
<p>//Edit: Codetags hinzugefügt (evilissimo)</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/138706/array-mit-strings-problem</link><generator>RSS for Node</generator><lastBuildDate>Sun, 30 Aug 2026 06:10:35 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/138706.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 28 Feb 2006 15:52:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Array mit Strings ----PROBLEM--- on Tue, 28 Feb 2006 23:50:51 GMT]]></title><description><![CDATA[<p>Hallo Forum,</p>
<p>ich möchte ein Array der Größe 3 mit 3 strings füllen, die ich dann nachher einzeln abfragen kann.</p>
<p>Wie mache ich das am besten.</p>
<p>Bisher habe ich eine Methode fillArray:</p>
<pre><code class="language-cpp">void fillArray () 
{ char* strings [2];
  char* name;
  int n;
  for (n=0; n&lt;2; n++) {
     cin &gt;&gt; name;
     strings[n]= name;
  }
}
</code></pre>
<p>Und wie steuere ich dann nachher die einzelnen Einträge an?</p>
<p>Vielen Dank<br />
Liebe Grüße<br />
Tina</p>
<p>//Edit: Codetags hinzugefügt (evilissimo)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1005201</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1005201</guid><dc:creator><![CDATA[Tina]]></dc:creator><pubDate>Tue, 28 Feb 2006 23:50:51 GMT</pubDate></item><item><title><![CDATA[Reply to Array mit Strings ----PROBLEM--- on Tue, 28 Feb 2006 16:06:54 GMT]]></title><description><![CDATA[<p>Hallo Tina,</p>
<p>so wie du das bisher machst wird das Ganze nicht funktionieren.<br />
Ich würde dir folgendes vorschlagen.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;
int main()
{
	char strings[3][255];
	char name[255];
    int i;
	for(i=0; i &lt; 3; i++)
	{
		cin &gt;&gt; name;
		strcpy(strings[i], name);
	}

	for (i = 0; i &lt; 3; i++)
		cout &lt;&lt; strings[i];
	return 0;
}
</code></pre>
<p>Wenn du allerdings mit der Standard Template Library schon Erfahrung hast, würde ich dir eine Liste und std::string empfehlen.</p>
<p>PS:<br />
Bitte benutze die Code Tags <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>Gruss<br />
Tobi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1005224</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1005224</guid><dc:creator><![CDATA[Tobias Gerg]]></dc:creator><pubDate>Tue, 28 Feb 2006 16:06:54 GMT</pubDate></item><item><title><![CDATA[Reply to Array mit Strings ----PROBLEM--- on Tue, 28 Feb 2006 18:26:44 GMT]]></title><description><![CDATA[<p>und vorallem nochmal den umgang mit char arrays durchgehen!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1005339</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1005339</guid><dc:creator><![CDATA[ser1al]]></dc:creator><pubDate>Tue, 28 Feb 2006 18:26:44 GMT</pubDate></item><item><title><![CDATA[Reply to Array mit Strings ----PROBLEM--- on Tue, 28 Feb 2006 21:11:46 GMT]]></title><description><![CDATA[<p>Hey Tobi<br />
Vielen Dank, das funktioniert so. Allerdings weiß ich nicht so ganz, wie ich jetzt auf das erste Element im Array zugreifen kann.</p>
<p>Wenn ich jetzt eine Methode habe, die mir den ersten Namen ausgeben soll, wie rufe ich das auf?</p>
<pre><code class="language-cpp">char* Array::getFirstName (){

char name = Null;
name = strings [1];
cout &lt;&lt; name &lt;&lt; endl;
return name;
}
</code></pre>
<p>Thanx nochmal und vielen Dank für die Hilfe....</p>
<p>Liebe Grüße Tina</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1005540</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1005540</guid><dc:creator><![CDATA[Tina]]></dc:creator><pubDate>Tue, 28 Feb 2006 21:11:46 GMT</pubDate></item><item><title><![CDATA[Reply to Array mit Strings ----PROBLEM--- on Tue, 28 Feb 2006 23:50:14 GMT]]></title><description><![CDATA[<p>arrays fangen bei 0 an zu zählen. das erste element eines array ist also immer array[0].</p>
<p>und in deiner funktion sind zwei dinge nicht so ganz sauber:</p>
<p>1. name ist nur ein char (ein einzelnes zeichen), kein array of char (string)</p>
<pre><code class="language-cpp">char *name = Null
</code></pre>
<p>;</p>
<p>2. du gibst die adresse einer lokalen variable zurück. das funktioniert mit glück, wenn der bereich im speicher nicht überschrieben wird. in der regel geht sowas aber schief. entweder machst du sowas mit call by reference</p>
<pre><code class="language-cpp">void Array::getFirstName(char *name)
{
 name = strings[0];
 cout &lt;&lt; name &lt;&lt; endl;
}
</code></pre>
<p>oder du benutzt nen pointer und lässt ihn auf den string zeigen</p>
<pre><code class="language-cpp">char* Array::getFirstName()
{
 char *name;
 name = strings[0];
 cout &lt;&lt; name &lt;&lt; endl;
 return name;
}
</code></pre>
<p>mit den beiden varianten erhältst du allerdings eine referenz auf die strings. das heisst änderungen an name wirken sich auch auf strings aus.</p>
<p>so bekommst du eine kopie:</p>
<pre><code class="language-cpp">char* Array::getFirstName()
{
 char *name = new char[strlen(strings[0]) + 1]; // speicher reservieren
 strcpy(name, strings[0]); // inhalt von strings[0] in den neuen speicher kopieren
 cout &lt;&lt; name &lt;&lt; endl;
 return name; // pointer auf neuen string zurückgeben
}
</code></pre>
<p>//Edit: Codetags hinzugefügt (evilissimo)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1005559</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1005559</guid><dc:creator><![CDATA[A---&amp;gt;b()]]></dc:creator><pubDate>Tue, 28 Feb 2006 23:50:14 GMT</pubDate></item><item><title><![CDATA[Reply to Array mit Strings ----PROBLEM--- on Wed, 01 Mar 2006 09:31:59 GMT]]></title><description><![CDATA[<p>Hier kann ich mich meinen Vorrednern nur anschließen...</p>
<p>Gruss<br />
Tobi</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1005733</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1005733</guid><dc:creator><![CDATA[Tobias Gerg]]></dc:creator><pubDate>Wed, 01 Mar 2006 09:31:59 GMT</pubDate></item></channel></rss>