<?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[myArray vs. &amp;amp;myArray]]></title><description><![CDATA[<p>Bei meiner Beschäftigung mit C++ ist mir wieder etwas tolles aufgefallen. Dazu erstmal der Code:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;typeinfo&gt;

using namespace std;

int main(void)
{
    int myArray[5];
    for(int i = 0; i &lt; 5; ++i)
    {
        myArray[i] = 0;
    }
    cout &lt;&lt; &amp;myArray &lt;&lt; endl;
    cout &lt;&lt; myArray &lt;&lt; endl;
    cout &lt;&lt; *(&amp;myArray) &lt;&lt; endl;
    cout &lt;&lt; *myArray &lt;&lt; endl;
    cout &lt;&lt; typeid(&amp;myArray).name() &lt;&lt; endl;
    cout &lt;&lt; typeid(myArray).name() &lt;&lt; endl;
}
</code></pre>
<p>Die Ausgabe ist schonmal erstaunlich:</p>
<pre><code>0x2ff220d8
0x2ff220d8
0x2ff220d8
0
int (*)[5]
int [5]
</code></pre>
<p>Um das Dilemma zusammenzufassen:</p>
<ol>
<li>Wundert es mich schonmal, dass man auf</li>
</ol>
<pre><code>myArray
</code></pre>
<p>den Adressoperator anwenden kann, da es ja eigentlich kein lvalue ist, man kann ihm jedenfalls (ohne den Indexoperator) nichts zuweisen.</p>
<ol start="2">
<li>Der Wert von</li>
</ol>
<pre><code>&amp;myArray
</code></pre>
<p>und</p>
<pre><code>myArray
</code></pre>
<p>ist identisch. Die Typen sind aber (offensichtlich) unterschiedlich.</p>
<ol start="3">
<li>Das erstaunlichste überhaupt: Obwohl die Adressen, die von</li>
</ol>
<pre><code>&amp;myArray
</code></pre>
<p>und</p>
<blockquote>
<p>myArray</p>
</blockquote>
<p>zurückgegeben werden, ja offensichtlich identisch sind, sind die Werte von</p>
<pre><code>*(&amp;myArray)
</code></pre>
<p>und</p>
<pre><code>*myArray
</code></pre>
<p>unterschiedlich.</p>
<p>Woran liegt das alles? Was passiert bei euren Compilern? Was sagt der Standard/ihr dazu?</p>
<p>Felix</p>
<p>P.S.: Ja, ich weiß, dass</p>
<pre><code>myArray
</code></pre>
<p>das Selbe ist wie</p>
<pre><code>&amp;(myArray[0])
</code></pre>
<p>. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/26a0.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--warning"
      title=":warning:"
      alt="⚠"
    /> Trotzdem ist das oben gezeigte Verhalten, insbesondere</p>
<pre><code>&amp;myArray
</code></pre>
<p>komisch</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/189257/myarray-vs-amp-myarray</link><generator>RSS for Node</generator><lastBuildDate>Fri, 25 Sep 2026 07:51:34 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/189257.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 10 Aug 2007 11:43:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to myArray vs. &amp;amp;myArray on Fri, 10 Aug 2007 11:43:33 GMT]]></title><description><![CDATA[<p>Bei meiner Beschäftigung mit C++ ist mir wieder etwas tolles aufgefallen. Dazu erstmal der Code:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;typeinfo&gt;

using namespace std;

int main(void)
{
    int myArray[5];
    for(int i = 0; i &lt; 5; ++i)
    {
        myArray[i] = 0;
    }
    cout &lt;&lt; &amp;myArray &lt;&lt; endl;
    cout &lt;&lt; myArray &lt;&lt; endl;
    cout &lt;&lt; *(&amp;myArray) &lt;&lt; endl;
    cout &lt;&lt; *myArray &lt;&lt; endl;
    cout &lt;&lt; typeid(&amp;myArray).name() &lt;&lt; endl;
    cout &lt;&lt; typeid(myArray).name() &lt;&lt; endl;
}
</code></pre>
<p>Die Ausgabe ist schonmal erstaunlich:</p>
<pre><code>0x2ff220d8
0x2ff220d8
0x2ff220d8
0
int (*)[5]
int [5]
</code></pre>
<p>Um das Dilemma zusammenzufassen:</p>
<ol>
<li>Wundert es mich schonmal, dass man auf</li>
</ol>
<pre><code>myArray
</code></pre>
<p>den Adressoperator anwenden kann, da es ja eigentlich kein lvalue ist, man kann ihm jedenfalls (ohne den Indexoperator) nichts zuweisen.</p>
<ol start="2">
<li>Der Wert von</li>
</ol>
<pre><code>&amp;myArray
</code></pre>
<p>und</p>
<pre><code>myArray
</code></pre>
<p>ist identisch. Die Typen sind aber (offensichtlich) unterschiedlich.</p>
<ol start="3">
<li>Das erstaunlichste überhaupt: Obwohl die Adressen, die von</li>
</ol>
<pre><code>&amp;myArray
</code></pre>
<p>und</p>
<blockquote>
<p>myArray</p>
</blockquote>
<p>zurückgegeben werden, ja offensichtlich identisch sind, sind die Werte von</p>
<pre><code>*(&amp;myArray)
</code></pre>
<p>und</p>
<pre><code>*myArray
</code></pre>
<p>unterschiedlich.</p>
<p>Woran liegt das alles? Was passiert bei euren Compilern? Was sagt der Standard/ihr dazu?</p>
<p>Felix</p>
<p>P.S.: Ja, ich weiß, dass</p>
<pre><code>myArray
</code></pre>
<p>das Selbe ist wie</p>
<pre><code>&amp;(myArray[0])
</code></pre>
<p>. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/26a0.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--warning"
      title=":warning:"
      alt="⚠"
    /> Trotzdem ist das oben gezeigte Verhalten, insbesondere</p>
<pre><code>&amp;myArray
</code></pre>
<p>komisch</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1342157</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1342157</guid><dc:creator><![CDATA[Phoemuex]]></dc:creator><pubDate>Fri, 10 Aug 2007 11:43:33 GMT</pubDate></item><item><title><![CDATA[Reply to myArray vs. &amp;amp;myArray on Fri, 10 Aug 2007 11:56:46 GMT]]></title><description><![CDATA[<p>Phoemuex schrieb:</p>
<blockquote>
<ol>
<li>Wundert es mich schonmal, dass man auf</li>
</ol>
<pre><code>myArray
</code></pre>
<p>den Adressoperator anwenden kann, da es ja eigentlich kein lvalue ist, man kann ihm jedenfalls (ohne den Indexoperator) nichts zuweisen.</p>
</blockquote>
<p>Die Frage des Zugriffs ist unerheblich. Benannte Objekte sind immer lvalues.</p>
<blockquote>
<ol start="2">
<li>Der Wert von</li>
</ol>
<pre><code>&amp;myArray
</code></pre>
<p>und</p>
<pre><code>myArray
</code></pre>
<p>ist identisch. Die Typen sind aber (offensichtlich) unterschiedlich.</p>
</blockquote>
<p>Genauer: Der Wert beider Pointer nachdem sie implizit in void* konvertiert wurden - nur das schaust du an. Es ist auch nicht überraschend: &amp;myArray zeigt auf das gesamte array, (int*)myArray dagegen nur auf das erste Element - und C++ verlangt ausdrücklich, dass der Speicher in Arrays kontinuierlich (also ohne Löcher) und mit aufsteigenden Adressen belegt wird.</p>
<blockquote>
<ol start="3">
<li>Das erstaunlichste überhaupt: Obwohl die Adressen, die von</li>
</ol>
<pre><code>&amp;myArray
</code></pre>
<p>und</p>
<blockquote>
<p>myArray</p>
</blockquote>
<p>zurückgegeben werden, ja offensichtlich identisch sind, sind die Werte von</p>
<pre><code>*(&amp;myArray)
</code></pre>
<p>und</p>
<pre><code>*myArray
</code></pre>
<p>unterschiedlich.</p>
</blockquote>
<p><em>&amp;myArray ist ein lvalue, dass auf das ganze Array verweist, also vom Typ int[5] - dafür gibt es keine Überladung mit cout, wohl aber für void</em> - es folgt also eine implizite Konvertierung in int* und dann in void*; welches ausgegeben wird. Bei *myArray ist es anders: * benötigt ein rvalue als Argument; es gibt aber keine lvalue-zu-rvalue-Konvertierung für Arrays, an dessen Stelle tritt die Array-zu-rvlaue-Pointer Konvertierung, das Ergebnis ist ein rvalue int* welches dereferenziert wird, *myArray ist also ein lvalue vom Typ int (welches auf das erste Element des Arrays verweist) - und das wird dann ganz normal ausgegeben, denn für int existiert eine passende Überladung des &gt;&gt; Operators.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1342165</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1342165</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Fri, 10 Aug 2007 11:56:46 GMT</pubDate></item><item><title><![CDATA[Reply to myArray vs. &amp;amp;myArray on Fri, 10 Aug 2007 12:30:51 GMT]]></title><description><![CDATA[<p>Vielen Dank <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /> Das lese ich mir jetzt noch 5 mal durch, bis ich es verstehe <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>Felix</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1342179</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1342179</guid><dc:creator><![CDATA[Phoemuex]]></dc:creator><pubDate>Fri, 10 Aug 2007 12:30:51 GMT</pubDate></item></channel></rss>