<?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[[gelöst] Variable gibt falschen Wert aus]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe ein Problem mit einer int-Variable die einen anderen Wert ausgibt als Sie haben sollte:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

int main( void ) {
    int array[10];
    int xMal = 9;

    for( size_t i = 0; i &lt;= 10; i++ ) {
        array[i] = (i+1) * xMal;
    }

    for( size_t i = 0; i &lt; (sizeof(array)/sizeof(int)); i++) {
        cout &lt;&lt; i+1 &lt;&lt; &quot; mal &quot; &lt;&lt; xMal &lt;&lt; &quot; = &quot; &lt;&lt; array[i] &lt;&lt; endl;
    }

    return 0;
}
</code></pre>
<p>Das Programm gibt mir bei der Ausführung das hier aus:</p>
<pre><code>1 mal 99 = 9
2 mal 99 = 18
3 mal 99 = 27
4 mal 99 = 36
5 mal 99 = 45
6 mal 99 = 54
7 mal 99 = 63
8 mal 99 = 72
9 mal 99 = 81
10 mal 99 = 90
</code></pre>
<p>Ich kann mir nicht erklären warum xMal den Wert 99 statt 9 ausgibt, witziger Weise wird der richtige Wert ausgegeben wenn ich xMal als const int definiere.</p>
<p>Ich benutze den g++ Compiler unter Linux.</p>
<p>Hat jemand eine Idee was da schief läuft?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/291494/gelöst-variable-gibt-falschen-wert-aus</link><generator>RSS for Node</generator><lastBuildDate>Mon, 17 Aug 2026 19:36:12 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/291494.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 19 Aug 2011 19:07:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to [gelöst] Variable gibt falschen Wert aus on Fri, 19 Aug 2011 19:22:07 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe ein Problem mit einer int-Variable die einen anderen Wert ausgibt als Sie haben sollte:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

using namespace std;

int main( void ) {
    int array[10];
    int xMal = 9;

    for( size_t i = 0; i &lt;= 10; i++ ) {
        array[i] = (i+1) * xMal;
    }

    for( size_t i = 0; i &lt; (sizeof(array)/sizeof(int)); i++) {
        cout &lt;&lt; i+1 &lt;&lt; &quot; mal &quot; &lt;&lt; xMal &lt;&lt; &quot; = &quot; &lt;&lt; array[i] &lt;&lt; endl;
    }

    return 0;
}
</code></pre>
<p>Das Programm gibt mir bei der Ausführung das hier aus:</p>
<pre><code>1 mal 99 = 9
2 mal 99 = 18
3 mal 99 = 27
4 mal 99 = 36
5 mal 99 = 45
6 mal 99 = 54
7 mal 99 = 63
8 mal 99 = 72
9 mal 99 = 81
10 mal 99 = 90
</code></pre>
<p>Ich kann mir nicht erklären warum xMal den Wert 99 statt 9 ausgibt, witziger Weise wird der richtige Wert ausgegeben wenn ich xMal als const int definiere.</p>
<p>Ich benutze den g++ Compiler unter Linux.</p>
<p>Hat jemand eine Idee was da schief läuft?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2108245</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2108245</guid><dc:creator><![CDATA[NilsV]]></dc:creator><pubDate>Fri, 19 Aug 2011 19:22:07 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] Variable gibt falschen Wert aus on Fri, 19 Aug 2011 19:10:28 GMT]]></title><description><![CDATA[<p>GCC unter Linux: <a href="http://ideone.com/I2bcm" rel="nofollow">http://ideone.com/I2bcm</a><br />
Funktioniert doch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2108246</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2108246</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Fri, 19 Aug 2011 19:10:28 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] Variable gibt falschen Wert aus on Fri, 19 Aug 2011 19:14:37 GMT]]></title><description><![CDATA[<p>Mach in Zeile 9 aus dem &quot;&lt;=&quot; ein &quot;&lt;&quot;, dann gibt g++ mir nicht mehr 99 aus.</p>
<p>array[10] gibt es ja bei dir nicht mehr, es sollte ja nur bis [9] laufen. Dein Rechner wird an dieser Speicherstelle wohl xMal gespeichert haben (also direkt hinter dem array). Durch &quot;&lt;=&quot; greifst du auf array[10] zu, was den Wert von xMal ersetzt (10+1 * 9 = 99)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2108247</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2108247</guid><dc:creator><![CDATA[*Cassiopeia*]]></dc:creator><pubDate>Fri, 19 Aug 2011 19:14:37 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] Variable gibt falschen Wert aus on Fri, 19 Aug 2011 19:21:37 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>GCC unter Linux: <a href="http://ideone.com/I2bcm" rel="nofollow">http://ideone.com/I2bcm</a><br />
Funktioniert doch.</p>
</blockquote>
<p>Ich nutze als IDE Netbeans, da trat das &quot;Problem&quot; nur auf wenn ich das Programm im Debug-Mode kompiliert habe. Sorry das meine Angaben so unpräzise waren.</p>
<p><sub>Cassiopeia</sub> schrieb:</p>
<blockquote>
<p>Mach in Zeile 9 aus dem &quot;&lt;=&quot; ein &quot;&lt;&quot;, dann gibt g++ mir nicht mehr 99 aus.</p>
<p>array[10] gibt es ja bei dir nicht mehr, es sollte ja nur bis [9] laufen. Dein Rechner wird an dieser Speicherstelle wohl xMal gespeichert haben (also direkt hinter dem array). Durch &quot;&lt;=&quot; greifst du auf array[10] zu, was den Wert von xMal ersetzt (10+1 * 9 = 99)</p>
</blockquote>
<p>Ups, der berühmte Zaunpfahl, da lag also das Problem. Vielen Dank für die Hilfe.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2108254</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2108254</guid><dc:creator><![CDATA[NilsV]]></dc:creator><pubDate>Fri, 19 Aug 2011 19:21:37 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] Variable gibt falschen Wert aus on Fri, 19 Aug 2011 22:43:02 GMT]]></title><description><![CDATA[<p>nein, das ist undefiniertes verhalten.<br />
du hast nur zufällig glück gehabt, dass hinter dem array direkt deine xMal variable liegt im speicher...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2108326</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2108326</guid><dc:creator><![CDATA[Skym0sh0]]></dc:creator><pubDate>Fri, 19 Aug 2011 22:43:02 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] Variable gibt falschen Wert aus on Fri, 19 Aug 2011 22:55:21 GMT]]></title><description><![CDATA[<p>314159265358979 schrieb:</p>
<blockquote>
<p>Funktioniert doch.</p>
</blockquote>
<p>Wenn du mit einem Runtime Error funktionieren meinst.<br />
Wieso 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>
]]></description><link>https://www.c-plusplus.net/forum/post/2108327</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2108327</guid><dc:creator><![CDATA[ROFLduhastLOLgesagt]]></dc:creator><pubDate>Fri, 19 Aug 2011 22:55:21 GMT</pubDate></item><item><title><![CDATA[Reply to [gelöst] Variable gibt falschen Wert aus on Sat, 20 Aug 2011 00:02:41 GMT]]></title><description><![CDATA[<p>Darauf hab ich jetzt nicht geachtet, aber 9 steht da.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2108334</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2108334</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Sat, 20 Aug 2011 00:02:41 GMT</pubDate></item></channel></rss>