[OCLUG-devel] sizeof an array of pointers

Dan Stromberg strombrg at dcs.nac.uci.edu
Mon Jun 20 10:27:32 PDT 2005


On Sun, 2005-06-19 at 19:27 -0700, Christopher Smith wrote:
> Doug Jolley wrote:
> 
> >I'm using a library routine that defines array as 'char **array'.  I
> >thought I  would be able to ascertain the size of the array (in bytes)
> >by using 'sizeof(array)'.  What I'm getting is '4' which is not the
> >size of the array in bytes.  It's not the number of elements in the
> >array either.  I think it MIGHT be the number of bytes in the pointer.
> > So, my question is:  What do I need to do to get the size of the
> >array in bytes?  My ultimate objective is to get the number of
> >elements in the array; but, I think that should be fairly easy after I
> >get the array size in bytes.
> >  
> >
> You are right that it's returning the size of a pointer to char*. There 
> is essentially no way to get the size of said array without some really 
> hairy runtime specific hacking. The compiler can't possibly know the 
> size at compile time, the runtime might know it depending on how memory 
> allocation is done, but C doesn't provide a standard (or even 
> semi-nonstandard) way to get at it. Normally libraries like this address 
> the issue either by terminating the array with a null pointer or have a 
> second argument somewhere that specifies the length of the array. I'd 
> check the docs on the library.

Would it not be the size of a single pointer to char **?  But anyway,
that should be the same size as pointer to char * pretty much always -
but I don't imagine it's guaranteed to be by the language.

Anyway, if you can declare your array as:

char array[10][20]

...then you'll likely find sizeof more helpful./

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://localhost.localdomain/pipermail/oclug-devel/attachments/20050620/46e3a9bc/attachment.bin 


More information about the OCLUG-devel mailing list