+2  A: 

I'm not sure what problem you are having with &array1. This C++ worked exactly as expected (all values the same)

int main(int argc, char* argv[])
{
    int array1[10];

    printf("%x %x\n", array1, &array1);
    cout << array1 << " " << &array1 << endl;

    void* ptr1 = array1;
    void* ptr2 = &array1;

    printf("%x %x\n", ptr1, ptr2);
    cout << ptr1 << " " << ptr2 << endl;
    return 0;
}
James Curran
The why is discussed here http://stackoverflow.com/questions/2528318/c-how-come-an-arrays-address-is-equal-to-its-value
torak
+5  A: 

If the array is in fact an array (which seems to be required for the sizeof to work), why don't you just use the simple macro:

#define MUNGE_THING( x ) munge_thing((void*)&(x), sizeof(x))

That should work both for arrays and structs:

int array[10];
assert( (void*)array == (void*)&array );

You have tagged the question as both C and C++, in C++ you can use templates and avoid the macros all together.

David Rodríguez - dribeas
You can't tell in munge_thing(p,n) if `p` points to `n` bytes or if `p` points to a pointer which points to `n` bytes.
Luther Blissett
Nathon
@David Rodríguez - dribeas: As I noted, this particular code has to compile as both C and C++; templates would make it usable for C++ only. For a C++ only project templates might be useful.
supercat
John Bode
David Rodríguez - dribeas
@David: The raw bytes are two indirections away if you pass an array, but only one indirection away if you pass the struct. For example, `void mungle(void*p,size_t n) { fwrite(p,n,1,some_file); }` writes the raw bytes from a struct, but rubbish from an array.
Luther Blissett
Unless there's away to tell how the actual data can be found, the pointer passed to mungle is useless.
Luther Blissett
Whoops, John, you're right. I got it backwards. I should have labeled the outputs of my little program.
Nathon
@Luther Blissett: what is the concrete definition of array that you are referring to? In a real array (not a block of memory allocated with `new []`, the address of the array and the array of the first element are the same value. `int array[10]; assert( (void*)array == (void*)(` Which is different from: `int *p = new int[10]; assert( (void*)p != (void*)`. In the first case it *is* a real array, in the second case it is a pointer. Arrays and pointers are *not* the same thing. -- Or maybe I am not understanding what you are trying to tell me.
David Rodríguez - dribeas
Oh I see the problem, I always had the degraded array case in mind. YOu are right of course. Taking an address of an non-degraded array should be ok. +1
Luther Blissett
A: 

Okay, I see my confusion. In C++, the type of &array is not compatible with the type of the array, and as the linked discussion notes, (&array)+1 is not the same as (array+1), but casting the unsubscripted pointers does in fact yield the proper results. The distinctions between arrays and pointers in C are very confusing. Thanks for the assistance.

supercat
The array and the first element are aligned in memory, so `
David Rodríguez - dribeas