On Tue, Sep 22, 2009 at 10:48 AM, Luke Benstead kazade@gmail.com wrote:
2009/9/22 Ben Klein shacklein@gmail.com:
2009/9/23 Luke Benstead kazade@gmail.com:
If it IS the case that this doesn't cause a crash and is perfectly valid, can someone explain to me how/why this works? Or point me (no pun intended) to the bit in the C spec that explains it? Coz the way I read it, it has to dereference dmW, otherwise how would the compiler find the address of the array? ... so confused :)
I believe it's because the array (as a pointer) is at the same location as start of the struct (as a pointer). Compiler then applies pointer arithmetic without dereferencing.
Ah, I see.. but in that case, how is an array different to using a pointer, like in Vitaliy's example? Surely that's the same thing essentially?
Luke.
It basically has to do with semantics. If you have ``char arr[5];'', the ``arr`` will refer to the address of the first element, ie ``arr==&arr''. The same applies for fixed size arrays in structs - because the array is allocated as part of the struct, ``dmW->dmFormName'' refers to the address of the 0th element (which as the compiler knows is ``dmW+__offset of dmFormName''), or as above ``dmW->dmFormName=&dmW->dmFormName''.
For the pointer case (``char *arr;''), ``arr'' would be an actual pointer - that is, ``&arr != arr'', since it stores an address and has a location of its own (whereas fixed length arrays just have a location).
This would make a nice interview question.
Mike.