TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Are pointers and arrays equivalent in C? (2009)

112 pointsby deanstagabout 10 years ago

13 comments

Animatsabout 10 years ago
You can actually pass an array as an array in C99:<p><pre><code> #include &lt;stdlib.h&gt; void f(size_t m, size_t n, float a[m][n]) { for (size_t i = 0; i &lt; m; i++) { for (size_t j=0; j &lt; n; j++) { a[i][j] = 0; } } } int main(int argc, char* argv[]) { float a[10][20]; f(10,20,a); } </code></pre> This &quot;conformant array&quot; feature was supported in C99. GCC supports it. Microsoft didn&#x27;t like it, though, and didn&#x27;t implement it, so nobody uses it, and it was made optional in later versions of the C standard.<p>It doesn&#x27;t do what it looks like it does. The last subscript is totally ignored by the compiler. For 1D arrays, it&#x27;s not only ignored, the object is still passed as a pointer to the element type.<p>I once proposed making arrays in C size-safe by taking conformant arrays seriously and adding some features to make them more useful.[1] After much discussion, the idea was considered sound, but not popular enough. So buffer overflows in C continue.<p>I have great hopes for Rust.<p>[1] <a href="http://animats.com/papers/languages/safearraysforc43.pdf" rel="nofollow">http:&#x2F;&#x2F;animats.com&#x2F;papers&#x2F;languages&#x2F;safearraysforc43.pdf</a>
评论 #9244026 未加载
评论 #9244316 未加载
krylonabout 10 years ago
When I was first learning C, pointers scared the crap out of me, and it took me longer to understand them than I am comfortable admitting to.<p>Then I got pointers and this big &quot;NOW I get it&quot;-moment and felt really clever.<p>And <i>then</i> came the moment I had a problem with pointers and arrays being interchangeable <i>most</i> of the time and became very confused about when they were not equivalent.<p>People who like C (which I do, too) will often point out C&#x27;s perceived simplicity when compared with, say, C++, but C definitely does have its murky corners, and the need for backward compatibility with existing code will most likely prevent them from getting cleaned up.
评论 #9246714 未加载
评论 #9243767 未加载
评论 #9244509 未加载
评论 #9244032 未加载
wsxcdeabout 10 years ago
One other difference on modern compilers relating specifically to character arrays:<p>This will work fine:<p><pre><code> char arr[] = &quot;stuff&quot;; printf(&quot;arr:%s\n&quot;, arr); arr[0] = &#x27;b&#x27;; arr[1] = &#x27;l&#x27;; printf(&quot;arr:%s\n&quot;, arr); </code></pre> arr is allocated on the stack (not heap, sorry about the mistake) and is mutable.<p>But the following will segfault:<p><pre><code> char* ptr = &quot;stuff&quot;; printf(&quot;ptr:%s\n&quot;, ptr); ptr[0] = &#x27;b&#x27;; ptr[1] = &#x27;1&#x27;; printf(&quot;ptr:%s\n&quot;, ptr); </code></pre> Strings are allocated in the read-only data segment. Technically you can only write const char* ptr = &quot;stuff&quot;, but due to wanting to remain backward compatibile, gcc will let you write the above and not even warn you unless you use -Wall or similar.
评论 #9245599 未加载
halayliabout 10 years ago
This is straight from Expert C Programming book, including the diagrams. It&#x27;s dishonest to recycle and present it as if it&#x27;s yours with a reference mention at the very bottom. This is not a reference, it&#x27;s a copy.
评论 #9244507 未加载
评论 #9244210 未加载
评论 #9246277 未加载
mpweiherabout 10 years ago
Also sizeof is different.<p><pre><code> char a[20]; char *b; </code></pre> sizeof a -&gt; the size of the array (20)<p>sizeof b -&gt; the size of the pointer (8 on my machine)
评论 #9246561 未加载
评论 #9245419 未加载
elibenabout 10 years ago
Funny how old articles resurface suddenly :-) It&#x27;s great that HN still likes technical content, though.<p>FWIW I also wrote a (shorter) follow-up post a few months later about multi-dimensional arrays and their relation to pointers-to-pointers: <a href="http://eli.thegreenplace.net/2010/04/06/pointers-vs-arrays-in-c-part-2d" rel="nofollow">http:&#x2F;&#x2F;eli.thegreenplace.net&#x2F;2010&#x2F;04&#x2F;06&#x2F;pointers-vs-arrays-i...</a>
userbinatorabout 10 years ago
I like to think of them as being different and the equivalence - in some cases - only comes from what is known as the &quot;array-pointer-decay&quot; semantic:<p><a href="http://www.lysator.liu.se/c/c-faq/c-2.html" rel="nofollow">http:&#x2F;&#x2F;www.lysator.liu.se&#x2F;c&#x2F;c-faq&#x2F;c-2.html</a><p>This is why &amp;a+n is not equivalent to a+n, something that a surprisingly large number of &quot;C pointer tutorials&quot; seem to get wrong.
sagoabout 10 years ago
Excellent. Years ago (15-20?) I wrote a program with the bug he mentions at the end. Way before Stack Overflow was a Google away. I could not work out why it crashed. I ended up rewriting it and it worked, but never understood what the bug was (in my mind it has been logged as &#x27;don&#x27;t extern arrays&#x27; ever since). I just read that and ah-ha!
je-soabout 10 years ago
Another interesting thing to know about pointer and arrays is:<p><pre><code> #include &lt;stdio.h&gt; typedef int a_t[100]; int main(void) { int a[100]; a_t* p = &amp;a; &#x2F;&#x2F; all printed pointer values are equal printf(&quot;%p %p %p %p\n&quot;, (void*)a, (void*)&amp;a, (void*)p, (void*)*p); return 0; }</code></pre>
golergkaabout 10 years ago
Will I be correct if I say that in the end, the difference is that arrays are immutable (in the address that they point to) and pointers are not?
评论 #9244043 未加载
评论 #9243513 未加载
评论 #9243515 未加载
justinmkabout 10 years ago
This is a well-presented version of <a href="http://c-faq.com/aryptr/" rel="nofollow">http:&#x2F;&#x2F;c-faq.com&#x2F;aryptr&#x2F;</a>.
wbsunabout 10 years ago
Aren&#x27;t everything equivalent in C?
评论 #9245391 未加载
kazinatorabout 10 years ago
Ah Betteridge&#x27;s law of headlines at work!<p>Arrays are aggregates of contiguously allocated objects.<p>Pointers are values indicating the storage locations of objects.<p>Thus, not equivalent.<p>Pointers are involved when you access an array. That doesn&#x27;t make it an array. Just like scanning written text on a piece of paper using your index finger does not mean that &quot;paper&quot; and &quot;your index finger&quot; are equivalent. That&#x27;s true even if you always instantiate an index finger before evaluating a piece of paper: co-ocurrence is not equivalence.
评论 #9243135 未加载
评论 #9243044 未加载