Hello,All!

I am now having a problem at using sizeof() function to ensure how many
items I have in my programe, the source code likes this:

const char* Data[]={"abc","bcd","cde","def","wot","shame"};

and now I were trying to use sizeof() to get the size:

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

but now the system told me that was illegal sizeof() caller, then I did some
change like this:

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

and

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

To my surprise, both of them were just get the size of the pointer, here in
my programme, they are 4, I mean sizeof( (char**) Data ),sizeof( (char*)
Data ) and sizeof( Data[0] ). so we can sure that they are all just a
pointer. How can I get teh correctly size of this function call and making
my szData = 6 ?

Is there anybody who can tell me that?

Thanks in advance!

Re: How can I get the size of this? by David

David
Fri Apr 22 02:51:51 CDT 2005


"Carl" <carl8421@126.com> wrote in message
news:uA09JQwRFHA.3336@TK2MSFTNGP10.phx.gbl...

> 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



Re: How can I get the size of this? by Carl

Carl
Fri Apr 22 03:09:50 CDT 2005

Well, I have gotten my problem.

Thanks very much.



Re: How can I get the size of this? by Victor

Victor
Fri Apr 22 08:36:39 CDT 2005

David Webber wrote:
> "Carl" <carl8421@126.com> wrote in message
> news:uA09JQwRFHA.3336@TK2MSFTNGP10.phx.gbl...
>
>
>>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.

<nitpick>
You can't do that all the time. With a type-id it has to be used with
parentheses.
</nitpick>

<rant>
Besides, many programmers just love parentheses everywhere. They even
put them around the expression in a return statement. And suddenly,
not surprisingly, though, it looks like a function! Yechhh!
</rant>

> [...]

V

Re: How can I get the size of this? by John

John
Fri Apr 22 12:08:10 CDT 2005

"David Webber" <dave@musical.demon.co.uk> wrote in message
news:e6%23IwAxRFHA.3704@TK2MSFTNGP12.phx.gbl
>
> 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).

I can't see why the use of a typedef should make the slightest
difference --- and on my system (VC++ 7.1) it doesn't. Indeed the OP's
original code works fine. The following gives a value of 6 for szData.

int main()
{
const char* Data[]={"abc","bcd","cde","def","wot","shame"};
int szData = sizeof( Data ) / sizeof( Data[0] );
return 0;
}


--
John Carson


Re: How can I get the size of this? by David

David
Fri Apr 22 12:23:33 CDT 2005


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:uwBJRB0RFHA.2384@tk2msftngp13.phx.gbl...

> David Webber wrote:
>> "Carl" <carl8421@126.com> wrote in message
>> news:uA09JQwRFHA.3336@TK2MSFTNGP10.phx.gbl...
>>
>>
>>>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.
>
> <nitpick>
> You can't do that all the time. With a type-id it has to be used
> with
> parentheses.
> </nitpick>

I know. That's why I said I *prefer* to do it, not that I *always*
do it.

> <rant>
> Besides, many programmers just love parentheses everywhere. They
> even
> put them around the expression in a return statement. And
> suddenly,
> not surprisingly, though, it looks like a function! Yechhh!
> </rant>

Again I *prefer* return without brackets - unless of course I'm
evaluating something in which the brackets make it clear that the
evaluation is done and then the return - even if it isn't completely
necessary.

But in the case of the sizeof operator, I find it much clearer if
I have a single token X which is well defined and just do

sizeof X;

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



Re: How can I get the size of this? by David

David
Fri Apr 22 13:23:23 CDT 2005


"John Carson" <jcarson_n_o_sp_am_@netspace.net.au> wrote in message
news:uKv0d31RFHA.3704@TK2MSFTNGP12.phx.gbl...

> I can't see why the use of a typedef should make the slightest
> difference and on my system (VC++ 7.1) it doesn't. Indeed the OP's
> original code works fine...

I must admit I didn't see why it shouldn't compile either. The
point I was making was that it sometimes aids clarity to be
absolutely pedantic. The expression

const char * Data[]

involves both * and [] operators, and though one may be reasonably
happy about these things, I still feel everything is instantly much
clearer if one has

LPCSTR Data[]

The * is wrapped up in the typedef and this declares an array of
LPCSTRs with no room for ambiguity.

The only way I can reconcile your experience with the original is
that it must be a different compiler. If that is the source of the
difference, then the writers of the other compiler were obviously
not as clear on the meaning of

const char * Data[]

as the writers of yours were :-)

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