Re: How can I get the size of this?




"Carl" <carl8421@xxxxxxx> wrote in message
news:uA09JQwRFHA.3336@xxxxxxxxxxxxxxxxxxxxxxx

> I am now having a problem at using sizeof() function

Don't think of it as a function - it isn't one. It's an operator.
I perefer to use it without brackets just to remind myself that it
is an operator.

Operating on something declared as a pointer returns the size of
the pointer.

Operating on something declared as an array returns the size of the
array (and not the size of the pointer you would have by using the
array name as a pointer).

So you have to be very clear which you have.

So for example in

> int szData = sizeof( (char*) Data )/ sizeof( Data[0] );

you are explicitly finding the ratio of the size of a char *
(unambiguously, because you have explicitly cast it) and dividing by
the size of a char *.

In these matters "typedef" is often your friend!

LPCSTR is already typedef'd as "const char *", so use it.

> LPCSTR Data[]={"abc","bcd","cde","def","wot","shame"};

makes Data unambiguously an array of LPCSTRs, and

int nSize = sizeof Data;

should get you the answer 24 (six times sizeof LPCSTR).


Dave
--
David Webber
Author MOZART the music processor for Windows -
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mzusers/mailinglist.htm


.



Relevant Pages

  • Re: [C or C++] Is this legal? sizeof *p
    ... where the size of an automatic array is defined at run time. ... > that case if sizeof is applied to a VLA, ... But applying sizeof to a VLA is applying ... > sizeof to an array, not a pointer, and using the name of an array as ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Poll: SizeOf(variable) vs SizeOf(type), should SizeOf(variable)bebanned?
    ... Using SizeOf on variables does not prevent programming mistakes. ... How will you solve the problem for pointer variables? ... For that, I use dynamic arrays, which case I use Length in conjunction with SizeOf to determine the amount of memory occupied by the array data. ...
    (alt.comp.lang.borland-delphi)
  • Re: De-referencing pointer to function-pointer
    ... >> Either your compiler is broken or you are not invoking it as a C ... >> is a constraint violation to apply the sizeof operator to a function ... >> pointer to the element type of the array. ... >> bytes, of the function, not of a pointer to the function. ...
    (comp.lang.c)
  • Re: A question on string literals
    ... and is why I said "or maybe even `sizeof context'". ... "pointer to T"; ... to first element of array". ... Chris Torek, Wind River Systems ...
    (comp.lang.c)
  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)