How to return the size of a structure array.. 
Author Message
 How to return the size of a structure array..

Hello guys,

        I am trying to write a function that returns the size of a
structure array.. (or number of arrays). Any of you know how to write
them?
I wrote it like this.. but it doesn't work..

int size(struct mn m[])
  {
  int i = 0;

  while(*m++)
    {
    i++;
    }
  return i;
  }

Please correct my errors..

thanks
Yue



Fri, 16 Jul 1999 03:00:00 GMT  
 How to return the size of a structure array..

Quote:

>Hello guys,
>    I am trying to write a function that returns the size of a
>structure array.. (or number of arrays). Any of you know how to write
>them?

Since the information is not passed to the function, the function has
little chance to return it :-(

Quote:
>I wrote it like this.. but it doesn't work..
>int size(struct mn m[])
>  {
>  int i = 0;

>  while(*m++)

A struct mn is _not_ a controling expression for a loop in C. Your
approach works for scalar types, and for 0-terminated vectors only.

If you insist on similar code for structs, adding an extra field
to your struct (e.g. "int valid;"), setting that field to a
non-zero value for _all_ elements of the array you are dealing
with, adding an extra "terminating" element with "valid" set
to zero, and changing the loop to

   while(m->valid)
   {
     ++m;
     ++i;
   }

is _one_ possible approach. As a stilistic issue, declaring size
as

   int size(struct nm *m);

will make this look less strange, imho.

Kurt

--
| Kurt Watzka                             Phone : +49-89-2180-6254



Sat, 17 Jul 1999 03:00:00 GMT  
 How to return the size of a structure array..

From the C FAQ:
"6.21:   Why doesn't sizeof properly report the size of an array when the
        array is a parameter to a function?

A:      The compiler pretends that the array parameter was declared as a
        pointer (see question 6.4), and sizeof reports the size of the
        pointer.

        References: H&S Sec. 7.5.2 p. 195."

In other words, all the function will receive is an address.  You do not
(in general) know that the last element of an array will be NULL.  In fact,
this would be very unusual.  One possible solution would be to put your
array inside a containing struct that had the count of elements.  Then you
could calculate total size by struct.count*sizeof element.



Quote:
> Hello guys,

>    I am trying to write a function that returns the size of a
> structure array.. (or number of arrays). Any of you know how to write
> them?
> I wrote it like this.. but it doesn't work..

> int size(struct mn m[])
>   {
>   int i = 0;

>   while(*m++)
>     {
>     i++;
>     }
>   return i;
>   }

> Please correct my errors..

> thanks
> Yue



Sat, 17 Jul 1999 03:00:00 GMT  
 How to return the size of a structure array..

Use the sizeof operator.
See Kernighan/Ritchie P135.



Sat, 17 Jul 1999 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. An array in a structure Or an Array Of Structures

2. Returning pointer to array of structure from function

3. Return type - array of structures - please help!

4. The size of the structure array

5. The size of the structure array

6. Largest Array of Structures Size Question

7. Having difficulty creating a function that returns an array of structures

8. The size of the structure array

9. returning variable size char arrays

10. Return Structure or return Pointer??

11. return an address VS return structure

12. Find size of variable size structure?

 

 
Powered by phpBB® Forum Software