<?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[Variable ändert sich selbstständig (?)]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich hab ein kleines Problem mit folgendem Code:</p>
<pre><code class="language-cpp">int area_width = 9, area_height = 9;
int area[9][9];
int x, y;

for(x=0;x&lt;=area_width;x++) {
	for(y=0;y&lt;=area_height;y++) {
		area[x][y] = 0;
	}
}
</code></pre>
<p>Wenn ich diesen Code ausführe, hat <strong>area_height</strong> am Ende der Schleife den Wert 0.</p>
<p>Was ich bisher versucht habe:<br />
1. <strong>area_height</strong> als Konstante deklarieren, also <strong>const int area_height = 9;</strong> Ergebnis war ein Laufzeitfehler.<br />
2. <strong>area_height</strong> probeweise überall durch die Zahl 9 ersetzen. Seltsamerweise nahm dann <strong>area_width</strong> den Wert 0 an. <strong>area_width</strong> war vorher aber immer 9 geblieben.<br />
3. Sowohl <strong>area_width</strong> als auch <strong>area_height</strong> durch 9 ersetzen. Ergebnis wie bei 1.</p>
<p>Wäre prima, wenn mir jemand helfen könnte. <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="🙂"
    /> Vielen Dank im Vorraus</p>
<p>Gruß<br />
dread</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/193168/variable-ändert-sich-selbstständig</link><generator>RSS for Node</generator><lastBuildDate>Sun, 27 Sep 2026 20:15:48 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/193168.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 23 Sep 2007 10:50:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Variable ändert sich selbstständig (?) on Sun, 23 Sep 2007 10:50:49 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich hab ein kleines Problem mit folgendem Code:</p>
<pre><code class="language-cpp">int area_width = 9, area_height = 9;
int area[9][9];
int x, y;

for(x=0;x&lt;=area_width;x++) {
	for(y=0;y&lt;=area_height;y++) {
		area[x][y] = 0;
	}
}
</code></pre>
<p>Wenn ich diesen Code ausführe, hat <strong>area_height</strong> am Ende der Schleife den Wert 0.</p>
<p>Was ich bisher versucht habe:<br />
1. <strong>area_height</strong> als Konstante deklarieren, also <strong>const int area_height = 9;</strong> Ergebnis war ein Laufzeitfehler.<br />
2. <strong>area_height</strong> probeweise überall durch die Zahl 9 ersetzen. Seltsamerweise nahm dann <strong>area_width</strong> den Wert 0 an. <strong>area_width</strong> war vorher aber immer 9 geblieben.<br />
3. Sowohl <strong>area_width</strong> als auch <strong>area_height</strong> durch 9 ersetzen. Ergebnis wie bei 1.</p>
<p>Wäre prima, wenn mir jemand helfen könnte. <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="🙂"
    /> Vielen Dank im Vorraus</p>
<p>Gruß<br />
dread</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1370971</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1370971</guid><dc:creator><![CDATA[dread]]></dc:creator><pubDate>Sun, 23 Sep 2007 10:50:49 GMT</pubDate></item><item><title><![CDATA[Reply to Variable ändert sich selbstständig (?) on Sun, 23 Sep 2007 10:55:17 GMT]]></title><description><![CDATA[<p>Hehe, witziger Fehler <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>
<p>Wenn du ein Array hast, das du mit <code>int a[9]</code> deklarierst, darfst du nur auf die Elemente a[0] bis [8] zugreifen. Da du auch auf das neunte zugreifst (wegen dem '&lt;=' in der Schleifenbedingung), überschreibst du die anderen Variablen damit <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>
]]></description><link>https://www.c-plusplus.net/forum/post/1370973</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1370973</guid><dc:creator><![CDATA[Badestrand]]></dc:creator><pubDate>Sun, 23 Sep 2007 10:55:17 GMT</pubDate></item><item><title><![CDATA[Reply to Variable ändert sich selbstständig (?) on Sun, 23 Sep 2007 10:56:37 GMT]]></title><description><![CDATA[<p>mach statt &lt;= bitte &lt; sonst hast beim letzten durchlauf, ein zugriff aufs nirvana, der rest klingt ein bisschen misteriös</p>
<p>mfg spjoe</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1370974</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1370974</guid><dc:creator><![CDATA[spjoe_logoff]]></dc:creator><pubDate>Sun, 23 Sep 2007 10:56:37 GMT</pubDate></item><item><title><![CDATA[Reply to Variable ändert sich selbstständig (?) on Sun, 23 Sep 2007 11:00:16 GMT]]></title><description><![CDATA[<p>Jap es liegt daran das der Index eines Arrays bei 0 und nicht bei 1 Anfängt. Dadurch hast du außerhalb des reservierten Bereiches geschrieben ...</p>
<pre><code class="language-cpp">const std::size_t area_width = 9, area_height = 9;
int area[area_width][area_height];

for (std::size_t x = 0; x &lt; area_width; ++x)
    for (std::size_t y = 0; y &lt; area_height; ++y)
        area[x][y] = 0;
</code></pre>
<p>...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1370976</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1370976</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Sun, 23 Sep 2007 11:00:16 GMT</pubDate></item><item><title><![CDATA[Reply to Variable ändert sich selbstständig (?) on Sun, 23 Sep 2007 11:56:52 GMT]]></title><description><![CDATA[<p>Japp, das war der Fehler. <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="🙂"
    /><br />
Werds mir merken.<br />
(Das kommt davon, wenn man von Visual Basic kommt)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1370993</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1370993</guid><dc:creator><![CDATA[dread]]></dc:creator><pubDate>Sun, 23 Sep 2007 11:56:52 GMT</pubDate></item><item><title><![CDATA[Reply to Variable ändert sich selbstständig (?) on Sun, 23 Sep 2007 19:36:24 GMT]]></title><description><![CDATA[<p>dread schrieb:</p>
<blockquote>
<p>...Das kommt davon, wenn man von Visual Basic kommt...</p>
</blockquote>
<p>Aber ich glaube nicht, dass in VB ein Array mit 9 Elemente 10 Elemente hat .... <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>dread schrieb:</p>
<blockquote>
<p>...</p>
<pre><code class="language-cpp">int area_width = 9, ...
int area[9][9];
...
for(x=0;x&lt;=area_width;...
</code></pre>
</blockquote>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1371231</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1371231</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Sun, 23 Sep 2007 19:36:24 GMT</pubDate></item><item><title><![CDATA[Reply to Variable ändert sich selbstständig (?) on Sun, 23 Sep 2007 20:02:50 GMT]]></title><description><![CDATA[<p>Simon2 schrieb:</p>
<blockquote>
<p>dread schrieb:</p>
<blockquote>
<p>...Das kommt davon, wenn man von Visual Basic kommt...</p>
</blockquote>
<p>Aber ich glaube nicht, dass in VB ein Array mit 9 Elemente 10 Elemente hat .... <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>Aber es ist schon irritierend: In VB gibt man bei der Initialisierung eines Arrays eben nicht die Größe eines Arrays sondern dessen höchsten Index (oder den niedrigsten und höchsten) an. Das heißt, folgende Initialisierung:</p>
<pre><code>Dim arr(10) As Integer
</code></pre>
<p>… erstellt (per Default) in Wahrheit ein Array mit 11 Elementen.</p>
<p>/EDIT: Und dadurch verwendet man in einer VB-Schleife eben doch eine *inklusive* obere Schranke, in C(++) hingegen eine exklusive.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1371247</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1371247</guid><dc:creator><![CDATA[Konrad Rudolph]]></dc:creator><pubDate>Sun, 23 Sep 2007 20:02:50 GMT</pubDate></item><item><title><![CDATA[Reply to Variable ändert sich selbstständig (?) on Sun, 23 Sep 2007 20:44:09 GMT]]></title><description><![CDATA[<p>Konrad Rudolph schrieb:</p>
<blockquote>
<p>...folgende Initialisierung:</p>
<pre><code>Dim arr(10) As Integer
</code></pre>
<p>… erstellt (per Default) in Wahrheit ein Array mit 11 Elementen.<br />
...</p>
</blockquote>
<p>Aha ! Dann hatte ich doch Unrecht mit:</p>
<p>Simon2 schrieb:</p>
<blockquote>
<p>...<br />
Aber ich glaube nicht, dass in VB ein Array mit 9 Elemente 10 Elemente hat .... <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>denn ich konnte mir eigentlich nur die 2 Varianten vorstellen:<br />
- Entweder gibt man die Anzahl der Array-Alemente an (und die Schleife [0, 9] durchläuft halt 10 Elemente)<br />
- oder den höchsten Index und zählt ab 1.</p>
<p>Auf die Idee &quot;Höchster Index UND ab 0 zählen&quot; bin ich gar nicht gekommen.</p>
<p>OK - wieder was gelernt. <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>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1371261</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1371261</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Sun, 23 Sep 2007 20:44:09 GMT</pubDate></item></channel></rss>