Re: malloc to static array

Joe Drew <[email protected]> Fri, 14 Oct 2005 23:37:00 -0400
Newsgroups gmane.comp.audio.mad.devel
Message-ID <[email protected]>
On Oct 13, 2005, at 7:55 PM, Steve Calfee wrote:
> This is really a C question. You could use either:
> decoder->sync = &syncDat[0];
> or
> decoder->sync = syncDat;
>
> And both of these assume that decoder->sync point to an array of  
> unsigned char, ie defined
> unsigned char *syncDat; or equivalent in a struct.

Further to what Steve and Andre have said:

C represents arrays as pointers; any pointer can be indexed as an  
array, and a reference to any array is a pointer. When you say syncDat 
[0], you're actually saying "Dereference the zeroth element of the  
array syncDat for me." & converts that back into a pointer, so  
&syncDat[0] means "dereference the zeroth element of syncDat, and  
then give me its address" - a no-op!

The reason syncDat and &syncDat[0] are the same is because syncDat  
already points at the zeroth element - it is just the memory address  
of the contiguous array in memory, and that memory address contains  
the zeroth element.

Understanding pointers and arrays in C is tough work, but once you've  
got it, you understand the language a lot more. Hope this shed some  
light on the subject for you.

Joe