
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