TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Are pointers and arrays equivalent in C?

65 点作者 r11t超过 15 年前

9 条评论

pmjordan超过 15 年前
A slight tangent, but something that surprised me: The [] operator literally is just syntactic sugar for pointer dereferencing and addition, so that the following equivalence holds:<p><pre><code> a[b] == *(a + b); </code></pre> The surprising consequence of this is that the following is legal:<p><pre><code> int foo[] = { 1, 2, 3 }; int bar = 2[foo]; /* yes, this evaluates to 3 */ </code></pre> because<p><pre><code> *(2 + foo) </code></pre> is legal, and + is commutative. This works for both pointers and arrays. I haven't worked out a practical use, though I suspect the C++ metaprogramming crowd might have. Obviously the equivalence only holds on the native types in C++ as [], + and * can each be separately overloaded.
评论 #903922 未加载
评论 #903796 未加载
评论 #904202 未加载
评论 #904731 未加载
keefe超过 15 年前
First off, I think is is an interesting article highlighting the difference between pointer arithmetic and array access. As far as the title goes, I think the answer should be a quick and obvious no. Pointers are variables that contain a particular memory address. Arrays are CONSECUTIVE pieces of memory where the variable name holds the address of the first piece of allocated memory. Conceptually, this is very different and the article does quite a nice job explaining the low level details of why you cannot gloss over this difference. The bit about arrays being converted to pointers in functions is left on my to read pile for now...
dimitar超过 15 年前
The answer to the same question here: <a href="http://c-faq.com/aryptr/aryptrequiv.html" rel="nofollow">http://c-faq.com/aryptr/aryptrequiv.html</a><p>``pointer arithmetic and array indexing [that] are equivalent in C, pointers and arrays are different.''
anamax超过 15 年前
No.<p>Array names are immutable. Pointer values may be mutable.<p>However, let's ignore that and consider "int a[10]; int * const p(a);"<p>sizeof(a) is the size of the array named a or 4 * sizeof(int). sizeof(p) is the size of the pointer named p.
评论 #904695 未加载
Goladus超过 15 年前
Arrays and pointers are not the same type. Pointers are used to access array elements, however the variable itself is not a pointer type. It's an array of some other type (which can also be an array). In addition to automatic memory allocation that happens with an array, this matters whenever compile-time type-checking happens.<p>So I guess it depends on what you mean by "equivalent."
parse_tree超过 15 年前
Also... Arrays have block scope. So if you have a function that declares an array, operates on it, then returns a pointer to that array, it's undefined behaviour.<p>This was very confusing to me when I learned C because it's one of those undefined behaviours that appears to work properly in trivial programs (at least with the compiler I was using).
评论 #904604 未加载
DarkShikari超过 15 年前
Another difference is that:<p><pre><code> array[-1] </code></pre> is not valid, while<p><pre><code> pointer[-1] </code></pre> can be valid, such as if it points to the middle of a data set.
评论 #904645 未加载
zouhair超过 15 年前
Some of you maybe interested in this course <a href="http://www.reddit.com/r/carlhprogramming" rel="nofollow">http://www.reddit.com/r/carlhprogramming</a>
评论 #904469 未加载
huhtenberg超过 15 年前
<p><pre><code> &#62; Here’s a quote from Expert C Programming: &#62; &#62; There is one difference between an array name and a &#62; pointer that must be kept in mind. A pointer is a &#62; variable, so pa=a and pa++ are legal. But an array &#62; name is not a variable; constructions like a=pa and &#62; a++ are illegal. </code></pre> That's deep.