[email protected] wrote:
> 
> On Thu, 2009-04-02 at 11:00 -0400, Jim Dougherty wrote:
>  >
>  >
>  > >
>  > > ----- Original Message -----
>  > > From: Michael Sullivan
>  > > To: [email protected] <mailto:c-prog%40yahoogroups.com> 
>  <mailto:c-prog%40yahoogroups.com>
>  > > Sent: Thursday, April 02, 2009 8:49 AM
>  > > Subject: [c-prog] How many elements in the array?
>  > >
>  > > The thread on returning arrays from a function raises a question to
>  > my
>  > > mind. Is there a way to determine how many elements are in the array
>  > > without explicitly passing the number of elements to the function? I
>  > > believe that in Java this is possible, but is it in C++? This is not
>  > a
>  > > homework question; just idle curiousity...
>  > >
>  >
>  > [email protected] <mailto:c-prog%40yahoogroups.com>  wrote:
>  > >
>  > > sizeof(array)/sizeof(array_type)
>  > >
>  > > Thanks,
>  > > Tyler Littlefield
>  >
>  > I think that the question was asking if the called function could
>  > determine the size of the array, not the calling function. I think
>  > that
>  > the answer is that the called function cannot determine the size of
>  > the
>  > array by itself, the size needs to be passed in.
> 
> Still, the called function would have a pointer to the passed-in array
> and would know its type, so couldn't it still do the sizeof thing?
>

If you use an array as an argument to sizeof then it gives you the total 
size of the array which is good, however the called function is given a 
pointer to an array it is not given an array.  If you use a pointer as 
an argument to sizeof the it will give you back 4 (or however big a 
pointer happens to be on your system).





Reply via email to