How can I get the size of this?



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!


.



Relevant Pages

  • Re: size of a function
    ... Note that "sizeof foo" won't do this, since the argument to sizeof is one of the contexts in which a function name is *not* implicitly converted to a pointer. ...
    (comp.lang.c)
  • Re: Pointer dereference rather than sizeof?
    ... One of them contained a gripe about the use of the sizeof ... operator as an argument to malloc calls. ... ptr = malloc); ... The trick here is that the pointer isn't actually dereferenced. ...
    (comp.lang.c)
  • 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: function pointers as function parameters
    ... > doesn't "decay" to a pointer to the function, ... Because this conversion does not occur, the operand of the sizeof ... only to make it a constraint violation ...
    (comp.lang.c)
  • Re: Few questinos
    ... ANSI C says you are allowed to use sizeof. ... Does this work for void type? ... but it won't set the corresponding pointer in the calling ... "Undefined behavior" includes the possibility that the implementation ...
    (comp.lang.c)

Loading