<?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[static Variable 0 setzen klappt nicht]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe hier ein Verhalten, das ich nicht verstehe:</p>
<pre><code class="language-cpp">void change_position(Widget widget, int shape)
{

    Window  win;
    Cursor  cursor;
    Pixmap pixmap1;
    Pixmap pixmap2;
    unsigned char *bits;
    int width; 
    int height;
    int xs, ys;
    static int lastShape[2];
    static int index = 0;

    static int numCalls = 1;

    printf(&quot;numCalls: %i\n&quot;, called++);

#ifdef APP
    return;
#endif
    printf(&quot;shape: %i, index: %i\n&quot;, shape, index);

     if (shape == C_SHAPE_LAST)
      shape = lastShape[index];
    else
      lastShape[index] = shape;

    index = !index;
</code></pre>
<p>index ist eine statische Variable, die immer zwischen 0 und 1 wechselt. Am Anfang hat index den Wert 0. numCalls ist zum Debuggen und steht fuer die Anzahl Aufrufe der Funktion.</p>
<p>Die Ausgabe ist:</p>
<blockquote>
<p>numCalls: 1<br />
shape: 3, index: 50011280</p>
</blockquote>
<p>Man sieht, die Funktion wurde gerade das 1. Mal aufgerufen, aber index hat den Wert 50011280, obwohl ich ihn auf 0 gesetzt habe. Wie kann das sein?!</p>
<p>Offenbar besteht das Problem nur bei der 0. Setze ich index auf 1 oder 2, dann wird auch 1 oder 2 geprinted. Wieso steht in index so ein Schrott, wenn ich es mit 0 initialisiere??</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/303062/static-variable-0-setzen-klappt-nicht</link><generator>RSS for Node</generator><lastBuildDate>Tue, 11 Aug 2026 00:01:40 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/303062.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 04 May 2012 12:15:53 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to static Variable 0 setzen klappt nicht on Fri, 04 May 2012 12:15:53 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich habe hier ein Verhalten, das ich nicht verstehe:</p>
<pre><code class="language-cpp">void change_position(Widget widget, int shape)
{

    Window  win;
    Cursor  cursor;
    Pixmap pixmap1;
    Pixmap pixmap2;
    unsigned char *bits;
    int width; 
    int height;
    int xs, ys;
    static int lastShape[2];
    static int index = 0;

    static int numCalls = 1;

    printf(&quot;numCalls: %i\n&quot;, called++);

#ifdef APP
    return;
#endif
    printf(&quot;shape: %i, index: %i\n&quot;, shape, index);

     if (shape == C_SHAPE_LAST)
      shape = lastShape[index];
    else
      lastShape[index] = shape;

    index = !index;
</code></pre>
<p>index ist eine statische Variable, die immer zwischen 0 und 1 wechselt. Am Anfang hat index den Wert 0. numCalls ist zum Debuggen und steht fuer die Anzahl Aufrufe der Funktion.</p>
<p>Die Ausgabe ist:</p>
<blockquote>
<p>numCalls: 1<br />
shape: 3, index: 50011280</p>
</blockquote>
<p>Man sieht, die Funktion wurde gerade das 1. Mal aufgerufen, aber index hat den Wert 50011280, obwohl ich ihn auf 0 gesetzt habe. Wie kann das sein?!</p>
<p>Offenbar besteht das Problem nur bei der 0. Setze ich index auf 1 oder 2, dann wird auch 1 oder 2 geprinted. Wieso steht in index so ein Schrott, wenn ich es mit 0 initialisiere??</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2208261</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208261</guid><dc:creator><![CDATA[Verwirrter2]]></dc:creator><pubDate>Fri, 04 May 2012 12:15:53 GMT</pubDate></item><item><title><![CDATA[Reply to static Variable 0 setzen klappt nicht on Fri, 04 May 2012 12:34:05 GMT]]></title><description><![CDATA[<p>Weil !index nicht das macht was du denkst.<br />
Entweder nimmst du einen bool und vertraust auf die automatische Konvertierung false-&gt;0 true-&gt;1, oder du weist explizit 1 bzw 0 zu:</p>
<pre><code class="language-cpp">index = index==0 ? 1 : 0;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2208265</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208265</guid><dc:creator><![CDATA[arghonaut]]></dc:creator><pubDate>Fri, 04 May 2012 12:34:05 GMT</pubDate></item><item><title><![CDATA[Reply to static Variable 0 setzen klappt nicht on Fri, 04 May 2012 12:48:44 GMT]]></title><description><![CDATA[<p>Variable zwischen 0 und 1 hin und her schalten:</p>
<pre><code class="language-cpp">index ^= 1;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2208273</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208273</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 04 May 2012 12:48:44 GMT</pubDate></item><item><title><![CDATA[Reply to static Variable 0 setzen klappt nicht on Fri, 04 May 2012 13:40:42 GMT]]></title><description><![CDATA[<p>Die Zeile <code>index = !index</code> (die im Übrigen sehr wohl genau das tut, was hier gewünscht ist) wurde doch noch gar nicht erreicht. Wieso gebt ihr so offensichtlich unsinnige Antworten?</p>
<p>Ich kann leider auch nicht helfen, eigentlich sollte das funktionieren. Mein Tipp wäre ja, dass das mal wieder nicht die ganze Wahrheit ist. Auffallend ist z.B. die Diskrepanz zwischen den Bezeichnern numCalls und called, die offenbar eigentlich dasselbe sein sollten ... bitte funktionierenden Code posten.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2208286</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208286</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Fri, 04 May 2012 13:40:42 GMT</pubDate></item><item><title><![CDATA[Reply to static Variable 0 setzen klappt nicht on Fri, 04 May 2012 16:05:02 GMT]]></title><description><![CDATA[<p>Wenn Index sowieso nur zwischen <code>1</code> und <code>0</code> wechselt, wieso nimmst du dann nicht gleich einen boolschen Typ? <code>index = !index</code> heißt weise <code>index</code> seinen negierten Wert zu. Die Negation von <code>0</code> ist <code>1.</code> Die Negation von <code>1</code> ist <code>0</code> . Genauso für boolsche Typen.</p>
<p>C++ Standard 5.3.1 Klausel 8 schrieb:</p>
<blockquote>
<p>The operand of the logical negation operator ! is implicitly converted to bool (clause 4); its value is true if the<br />
converted operand is false and false otherwise. The type of the result is bool.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2208318</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208318</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Fri, 04 May 2012 16:05:02 GMT</pubDate></item><item><title><![CDATA[Reply to static Variable 0 setzen klappt nicht on Fri, 04 May 2012 16:03:23 GMT]]></title><description><![CDATA[<p>Also: mit der Initialisierung mit static int index = 0 und index = !index zum Alternieren (0,1) ist alles ok. Der Bock liegt deshalb woanders!</p>
<p>edit: ob das nun mit int oder bool gemacht wird, ändert nichts! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2208320</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2208320</guid><dc:creator><![CDATA[berniebutt]]></dc:creator><pubDate>Fri, 04 May 2012 16:03:23 GMT</pubDate></item></channel></rss>