News for hackers. Stuff that matters.
Re:Are pointers and arrays equivalent in C? (2009) by kazinator on Saturday March 21, 2015 @11:26AM (#9242967)
Ah Betteridge's law of headlines at work!

Arrays are aggregates of contiguously allocated objects.

Pointers are values indicating the storage locations of objects.

Thus, not equivalent.

Pointers are involved when you access an array. That doesn't make it an array. Just like scanning written text on a piece of paper using your index finger does not mean that "paper" and "your index finger" are equivalent. That's true even if you always instantiate an index finger before evaluating a piece of paper: co-ocurrence is not equivalence.

Re:Are pointers and arrays equivalent in C? (2009) by gnuvince on Saturday March 21, 2015 @12:17PM (#9243135)
Why is this comment being downvoted? The explanation is correct and the analogy quite good too!
Re:Are pointers and arrays equivalent in C? (2009) by tomku on Saturday March 21, 2015 @02:38PM (#9243734)
Because it's explaining the wrong confusion. People who are confused about pointers and arrays in C don't think that the concept of a pointer is the same as the concept of an array - they think that in C, the two have the same semantics. That explanation and analogy do nothing to explain the semantic difference, and honestly come off as a pretty condescending by essentially saying "Well they're different things, of course they're different!"

The poster probably did not help matters after that by calling the downvoters "clueless n00bs" or making a self-righteous statement about how they won't delete the comment just because of downvotes. That kind of toxic behavior just ensures that you'll get more downvotes.

Re:Are pointers and arrays equivalent in C? (2009) by kazinator on Saturday March 21, 2015 @12:57PM (#9243308)
Because the number of people who believe that arrays are pointers vastly outnumbers those who don't, and many of those clueless n00bs have karma >= 500 on HN, or whatever the level is to be able to downvote. Judging by one answer, some people don't believe that the comment could even be about C, so wrong it must be!

Nope; I simply refuse to delete a comment that is civil and correct just because of some fluffy downvotes. It stays and that's that.

Re:Are pointers and arrays equivalent in C? (2009) by DanBC on Saturday March 21, 2015 @12:28PM (#9243184)
I suspect it's downvoted for the mention of Betteridges Law of Headlines, which (rightly) earns downvotes whenever it's mentioned.
Re:Are pointers and arrays equivalent in C? (2009) by kazinator on Saturday March 21, 2015 @01:03PM (#9243328)
Look, it even worked for your comment! Must be true.
Re:Are pointers and arrays equivalent in C? (2009) by TeMPOraL on Saturday March 21, 2015 @11:46AM (#9243044)
The key part of the headline here is "in C". In C, it's known that "name of the array is the address to its first element" which is one of the things that lead people into thinking that pointers and arrays are equivalent - in C.
Re:Are pointers and arrays equivalent in C? (2009) by kazinator on Saturday March 21, 2015 @12:18PM (#9243142)
No, the name of an array is not the address to its first element.

For instance, this equivalence is not required:

   (sizeof array) == (sizeof &array[0])
though it could be true by coincidence.

If array were the address of the first element, then it would always have the same size. Be careful with words like "is".

Oh, the above is "in C", by the way. We wouldn't want not to satisfy your key point!

Re:Are pointers and arrays equivalent in C? (2009) by MaulingMonkey on Saturday March 21, 2015 @12:59PM (#9243315)
Quite right.

A set of interesting equalities:

    (void*)&array   == (void*)&array[0]   // likely true
    (void*)&pointer == (void*)&pointer[0] // likely false (although it could be true by coincidence)
A pointer to an array is the address of the first element.

The name of an array can be trivially and implicitly converted to the address to it's first element, but this conversion creates a (temporary) pointer, it does not make the array itself a pointer!

The array (nor it name) is not in and of itself storage for any such address, it is no pointer, and as a result &array won't give you back a pointer-to-pointer because there's no underlying 4-8 byte storage for the underlying pointer - there's no permanent pointer to point to.

Re:Are pointers and arrays equivalent in C? (2009) by arielby on Saturday March 21, 2015 @01:19PM (#9243398)
According to the C FAQ, Whenever an array appears in an expression, the compiler implicitly generates a pointer to the array's first element, just as if the programmer had written &a[0]. The exceptions are when the array is the operand of a sizeof or & operator, or is a string literal initializer for a character array.
Re:Are pointers and arrays equivalent in C? (2009) by kazinator on Saturday March 21, 2015 @01:09PM (#9243353)
Sure:

   (void*)&array   == (void*)&array[0]   // likely true
That is true by definition. The address of the first element is the same as that of the array, period. They cannot convert to different void pointers.
Re:Are pointers and arrays equivalent in C? (2009) by pqomdv on Saturday March 21, 2015 @01:25PM (#9243423)
Which definition is that?

You probably though of:

  array  == &array[0]
which is always true.

This:

  (void*)&array   == (void*)&array[0]
which simplifies to:

    (void*)&array   == (void*)array
is not.

C doesn't guarantee that the values of &array and array are the same. The types T( * )[ * ] and T* are not even compatible and those two pointers are allowed to have different sizes and representation.

Re:Are pointers and arrays equivalent in C? (2009) by kazinator on Saturday March 21, 2015 @01:39PM (#9243478)
The first element of an array is located at the address of an array. Even if the pointer-to-array and pointer-to-element types have different size and representation, they point to the same place. The resulting pointer-to-void converted values must point to that place, and have the same type.

This relationship is also true between a pointer to a struct and a pointer to the first element. And also among pointers to the elements of a union.

Re:Are pointers and arrays equivalent in C? (2009) by MaulingMonkey on Saturday March 21, 2015 @01:30PM (#9243438)
Granted, we've been presupposing C, but:

In C++ you have a simple out by overloading T::operator&, allowing you to alter the result of the RHS of that equation and break it.

There's also a little bit in the standard prohibiting comparing pointers to different arrays - a leftover from the 16-bit era of near and far pointers I suspect - but that appears to only apply to relational comparisons in the C++11 standard:

http://stackoverflow.com/questions/9086372/how-to-compare-po...

I either misremember this applying to == and != as well (likely), or this changed since the C++03 standard that I'm used to referencing (unlikely). I'm also unsure if C has an equivalent rule. A fun note from that SO link:

    std::less<T*>
guarantees more than

    operator<( T*, T* )
enabling

    std::map<T*,...>
et all.

(edit: formatting)

Re:Are pointers and arrays equivalent in C? (2009) by EpicEng on Saturday March 21, 2015 @12:42PM (#9243248)
...and another one bites the dust...