<?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[2Darray.]]></title><description><![CDATA[<p>Hallo an alle.<br />
Seit Tagen versuche ich eine 2D Array zu initialisieren in einer funktion.<br />
z.B: das geht. iArray ist globale variable.</p>
<pre><code class="language-cpp">char iArray[2][2] = {{'s','s'},{'a','a'}};
</code></pre>
<p>Wenn ich die bei deklaration gleich defeniere.<br />
Aber wenn ich die nur deklarire und in eine funktion defenieren will bekomme ich<br />
fehler.<br />
Fehlerhafter code:</p>
<pre><code class="language-cpp">char iArray[2][2];
.............
.............
void iFunk()
{
    iArray[2][2] = {{'s','s'},{'a','a'}};
}
</code></pre>
<p>Compiler Fehler: error: expected primary-expression before '{' token.<br />
Ich Programmiere mit Code::blocks unter Windows.</p>
<p>Danke.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/293880/2darray</link><generator>RSS for Node</generator><lastBuildDate>Sun, 16 Aug 2026 04:04:16 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/293880.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 12 Oct 2011 16:41:28 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to 2Darray. on Wed, 12 Oct 2011 16:41:28 GMT]]></title><description><![CDATA[<p>Hallo an alle.<br />
Seit Tagen versuche ich eine 2D Array zu initialisieren in einer funktion.<br />
z.B: das geht. iArray ist globale variable.</p>
<pre><code class="language-cpp">char iArray[2][2] = {{'s','s'},{'a','a'}};
</code></pre>
<p>Wenn ich die bei deklaration gleich defeniere.<br />
Aber wenn ich die nur deklarire und in eine funktion defenieren will bekomme ich<br />
fehler.<br />
Fehlerhafter code:</p>
<pre><code class="language-cpp">char iArray[2][2];
.............
.............
void iFunk()
{
    iArray[2][2] = {{'s','s'},{'a','a'}};
}
</code></pre>
<p>Compiler Fehler: error: expected primary-expression before '{' token.<br />
Ich Programmiere mit Code::blocks unter Windows.</p>
<p>Danke.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2130613</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2130613</guid><dc:creator><![CDATA[runix81]]></dc:creator><pubDate>Wed, 12 Oct 2011 16:41:28 GMT</pubDate></item><item><title><![CDATA[Reply to 2Darray. on Wed, 12 Oct 2011 16:50:26 GMT]]></title><description><![CDATA[<p>Schreib mal nen typ vor das array in der funktion.<br />
Also sollte dann so aussehen:</p>
<pre><code class="language-cpp">char iArray[2][2]; 
 ............. 
 ............. 
void iFunk() 
 { 
     char iArray[2][2] = {{'s','s'},{'a','a'}}; 
 }
</code></pre>
<p>aber das wird so auch nicht funktionieren, weil du das array global deklariert hast und dann lokal definieren willst.<br />
Warum willst du das so machen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2130617</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2130617</guid><dc:creator><![CDATA[*Q* 1]]></dc:creator><pubDate>Wed, 12 Oct 2011 16:50:26 GMT</pubDate></item><item><title><![CDATA[Reply to 2Darray. on Wed, 12 Oct 2011 16:51:11 GMT]]></title><description><![CDATA[<p>Du kannst die Array-Initialisierungsliste nur während der Initialisierung benutzen, nachträglich musst du die Elemente einzeln zuweisen (oder von einem bestehenden Array kopieren).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2130619</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2130619</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Wed, 12 Oct 2011 16:51:11 GMT</pubDate></item><item><title><![CDATA[Reply to 2Darray. on Wed, 12 Oct 2011 17:17:00 GMT]]></title><description><![CDATA[<p>Ich will eine consolen Spiel programmieren und das Array ist ein Spielfeld<br />
die eigentlich iArray[20][30] ist. Ich überlege mir ob ich vileicht stat array ein oder mehrere string's verwenden, oder eine andere lösung mal schauen, aber Danke an all.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2130624</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2130624</guid><dc:creator><![CDATA[runix81]]></dc:creator><pubDate>Wed, 12 Oct 2011 17:17:00 GMT</pubDate></item><item><title><![CDATA[Reply to 2Darray. on Wed, 12 Oct 2011 18:25:37 GMT]]></title><description><![CDATA[<p>Wirf mal einen Blick in die STL-Container. Wenn ich mich richtig erinnere, gab es sogar im Forum ein Artikel dazu.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2130655</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2130655</guid><dc:creator><![CDATA[*Rewind*]]></dc:creator><pubDate>Wed, 12 Oct 2011 18:25:37 GMT</pubDate></item><item><title><![CDATA[Reply to 2Darray. on Thu, 13 Oct 2011 07:29:56 GMT]]></title><description><![CDATA[<p>runix81 schrieb:</p>
<blockquote>
<p>...und das Array ist ein Spielfeld...</p>
</blockquote>
<p>Dann kannst Du dir doch auch gleich schonmal angewöhnen, ohne globale Variablen auszukommen und den Zustand gleich als Klasse zu verpacken</p>
<p>zustand.hpp</p>
<pre><code class="language-cpp">#ifndef ZUSTAND_HPP_INCLUDED
#define ZUSTAND_HPP_INCLUDED

#include &lt;cassert&gt;

namespace meinspiel {

const int feld_hoehe = 30;
const int feld_breite = 20;

struct zustand
{
  typedef char zeilentyp[feld_breite];

  zeilentyp feld[feld_hoehe];

  zustand();

  zeilentyp const&amp; operator[](int idx) const
  { assert(0&lt;=idx &amp;&amp; idx&lt;feld_hoehe); return feld[idx]; }

  zeilentyp      &amp; operator[](int idx)
  { assert(0&lt;=idx &amp;&amp; idx&lt;feld_hoehe); return feld[idx]; }
};

} // namespace meinspiel

#endif
</code></pre>
<p>zustand.cpp</p>
<pre><code class="language-cpp">#include &lt;algorithm&gt;
#include &quot;zustand.hpp&quot;

namespace meinspiel {

zustand::zustand()
{
  for (int y=0; y&lt;feld_hoehe; ++y)
    std::fill(feld[y]+0,feld[y]+feld_breite,' ');
}

} // namespace meinspiel
</code></pre>
<p>main.cpp</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &quot;zustand.hpp&quot;

int main()
{
  meinspiel::zustand z;
  ...usw...
  return 0;
}
</code></pre>
<p>...oder so ähnlich...</p>
<p>Falls Dein Compiler std::array unterstützt (aus dem &lt;array&gt; Header), solltest Du das nehmen</p>
<pre><code class="language-cpp">struct zustand
{
  typedef std::array&lt;char,feld_breite&gt; zeilentyp;

  std::array&lt;zeilentyp,feld_hoehe&gt; feld;
</code></pre>
<p>Vorteil: Der &quot;Debug-Modus&quot; der Klasse könnte eine Bereichsüberprüfung anbieten, womit Du Indizierungsfehler früher erkennen würdest.</p>
<p>Falls die Spielfeldgröße sehr groß wird oder erst zur Laufzeit festgelegt wird, kannst Du statt &quot;rohen Feldern&quot; einfach std::vector verwenden.</p>
<p>kk</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2130819</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2130819</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Thu, 13 Oct 2011 07:29:56 GMT</pubDate></item></channel></rss>