Velocity Reviews > size of an array (number of elements)

# size of an array (number of elements)

Grey Alien
Guest
Posts: n/a

 07-04-2007
If I have a struct declared as :

struct A
{
double x ;
char name[LONG_ENOUGH];
struct Other other ;
void * ptr ;
};

And I have an array of these structs, how can I determine the number of
items in the array. I need to be able to determine this since I have a
function with signature:

void foo(struct A array_[])
{
//Process each of the elements in the passed array
}

Harald van =?UTF-8?B?RMSzaw==?=
Guest
Posts: n/a

 07-04-2007
Grey Alien wrote:
> If I have a struct declared as :
>
> struct A
> {
> double x ;
> char name[LONG_ENOUGH];
> struct Other other ;
> void * ptr ;
> };
>
>
> And I have an array of these structs, how can I determine the number of
> items in the array.

In general, you can get the size of an array x by calculating
sizeof x / sizeof *x. However, see below.

> I need to be able to determine this since I have a
> function with signature:
>
> void foo(struct A array_[])
> {
> //Process each of the elements in the passed array
> }

Inside foo, you do not have any array of struct A. You have a pointer to
struct A. From this pointer, it is not possible to reconstruct the size of
any array it might point to. Either tell it directly, using an extra
parameter, or put some marker in struct A.

Walter Roberson
Guest
Posts: n/a

 07-04-2007
In article <(E-Mail Removed)>,
Grey Alien <(E-Mail Removed)> wrote:
>If I have a struct declared as :

>struct A
>{
> double x ;
> char name[LONG_ENOUGH];
> struct Other other ;
> void * ptr ;
>};

>And I have an array of these structs, how can I determine the number of
>items in the array. I need to be able to determine this since I have a
>function with signature:

>void foo(struct A array_[])
>{
> //Process each of the elements in the passed array
>}

You can't do it inside the function; you have to pass the number
of elements in.

At the scope that declares the variable, you can use
sizeof(TheArray) / sizeof(TheArray[0])
(unless, that is, that TheArray is dynamically allocated storage.)
--
All is vanity. -- Ecclesiastes

Flash Gordon
Guest
Posts: n/a

 07-04-2007
Grey Alien wrote, On 04/07/07 19:24:
> If I have a struct declared as :
>
> struct A
> {
> double x ;
> char name[LONG_ENOUGH];
> struct Other other ;
> void * ptr ;
> };
>
>
> And I have an array of these structs, how can I determine the number of
> items in the array.

That would be question 6.23 of the comp.lang.c FAQ were you dealing with
an array, but...

> I need to be able to determine this since I have a
> function with signature:
>
> void foo(struct A array_[])

You should read question 6.4 of the comp.lang.c FAQ as well to see why
you do *not* have an array in here.

> {
> //Process each of the elements in the passed array
> }

Change the signature to pass in the size of have a sentinal value at the
end of the array.
--
Flash Gordon

CBFalconer
Guest
Posts: n/a

 07-04-2007
Grey Alien wrote:
>
> If I have a struct declared as :
>
> struct A {
> double x ;
> char name[LONG_ENOUGH];
> struct Other other ;
> void * ptr ;
> };
>
> And I have an array of these structs, how can I determine the
> number of items in the array. I need to be able to determine this
> since I have a function with signature:
>
> void foo(struct A array_[]) {
> //Process each of the elements in the passed array
> }

You look closely at the (incomplete) definition of the struct,
laboriously count the objects identified, and use that value. In
this case the value would be 4.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Flash Gordon
Guest
Posts: n/a

 07-05-2007
CBFalconer wrote, On 05/07/07 00:53:
> Grey Alien wrote:
>> If I have a struct declared as :
>>
>> struct A {
>> double x ;
>> char name[LONG_ENOUGH];
>> struct Other other ;
>> void * ptr ;
>> };
>>
>> And I have an array of these structs, how can I determine the
>> number of items in the array. I need to be able to determine this
>> since I have a function with signature:
>>
>> void foo(struct A array_[]) {
>> //Process each of the elements in the passed array
>> }

>
> You look closely at the (incomplete) definition of the struct,
> laboriously count the objects identified, and use that value. In
> this case the value would be 4.

Chuck, I don't know where you get 4 from (well, I can guess) in the
above since the OP wants to iterate over the array, not over the fields
in the struct. Looking at the struct definition does not give you any
clue about how many elements the array has.
--
Flash Gordon

Keith Thompson
Guest
Posts: n/a

 07-05-2007
CBFalconer <(E-Mail Removed)> writes:
> Grey Alien wrote:
>> If I have a struct declared as :
>>
>> struct A {
>> double x ;
>> char name[LONG_ENOUGH];
>> struct Other other ;
>> void * ptr ;
>> };
>>
>> And I have an array of these structs, how can I determine the
>> number of items in the array. I need to be able to determine this
>> since I have a function with signature:
>>
>> void foo(struct A array_[]) {
>> //Process each of the elements in the passed array
>> }

>
> You look closely at the (incomplete) definition of the struct,
> laboriously count the objects identified, and use that value. In
> this case the value would be 4.

Huh? That's the number of members in the structure. He was clearly

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

CBFalconer
Guest
Posts: n/a

 07-05-2007
Flash Gordon wrote:
> CBFalconer wrote, On 05/07/07 00:53:
>> Grey Alien wrote:
>>
>>> If I have a struct declared as :
>>>
>>> struct A {
>>> double x ;
>>> char name[LONG_ENOUGH];
>>> struct Other other ;
>>> void * ptr ;
>>> };
>>>
>>> And I have an array of these structs, how can I determine the
>>> number of items in the array. I need to be able to determine this
>>> since I have a function with signature:
>>>
>>> void foo(struct A array_[]) {
>>> //Process each of the elements in the passed array
>>> }

>>
>> You look closely at the (incomplete) definition of the struct,
>> laboriously count the objects identified, and use that value. In
>> this case the value would be 4.

>
> Chuck, I don't know where you get 4 from (well, I can guess) in
> the above since the OP wants to iterate over the array, not over
> the fields in the struct. Looking at the struct definition does
> not give you any clue about how many elements the array has.

Yes it does. There is one double, one char array, one struct
(undefined), and one void* pointer. That makes 4 objects in the
struct to me.

If he wants to know how big the array of struct A is he will have
to pass that value in. It is not calculable in the called
function. That is a separate question.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Keith Thompson
Guest
Posts: n/a

 07-05-2007
CBFalconer <(E-Mail Removed)> writes:
> Flash Gordon wrote:
>> CBFalconer wrote, On 05/07/07 00:53:
>>> Grey Alien wrote:
>>>> If I have a struct declared as :
>>>>
>>>> struct A {
>>>> double x ;
>>>> char name[LONG_ENOUGH];
>>>> struct Other other ;
>>>> void * ptr ;
>>>> };
>>>>
>>>> And I have an array of these structs, how can I determine the
>>>> number of items in the array. I need to be able to determine this
>>>> since I have a function with signature:
>>>>
>>>> void foo(struct A array_[]) {
>>>> //Process each of the elements in the passed array
>>>> }
>>>
>>> You look closely at the (incomplete) definition of the struct,
>>> laboriously count the objects identified, and use that value. In
>>> this case the value would be 4.

>>
>> Chuck, I don't know where you get 4 from (well, I can guess) in
>> the above since the OP wants to iterate over the array, not over
>> the fields in the struct. Looking at the struct definition does
>> not give you any clue about how many elements the array has.

>
> Yes it does. There is one double, one char array, one struct
> (undefined), and one void* pointer. That makes 4 objects in the
> struct to me.
>
> If he wants to know how big the array of struct A is he will have
> to pass that value in. It is not calculable in the called
> function. That is a separate question.

Yes, and it's the question he asked. "And I have an array of these
structs, how can I determine the number of items in the array."

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Flash Gordon
Guest
Posts: n/a

 07-05-2007
CBFalconer wrote, On 05/07/07 07:32:
> Flash Gordon wrote:
>> CBFalconer wrote, On 05/07/07 00:53:
>>> Grey Alien wrote:
>>>
>>>> If I have a struct declared as :
>>>>
>>>> struct A {
>>>> double x ;
>>>> char name[LONG_ENOUGH];
>>>> struct Other other ;
>>>> void * ptr ;
>>>> };
>>>>
>>>> And I have an array of these structs, how can I determine the
>>>> number of items in the array. I need to be able to determine this

^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>> since I have a function with signature:
>>>>
>>>> void foo(struct A array_[]) {
>>>> //Process each of the elements in the passed array

^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>>>> }
>>> You look closely at the (incomplete) definition of the struct,
>>> laboriously count the objects identified, and use that value. In
>>> this case the value would be 4.

>> Chuck, I don't know where you get 4 from (well, I can guess) in
>> the above since the OP wants to iterate over the array, not over

^^^^^^^^^^^^^^^^^^^^^^
>> the fields in the struct. Looking at the struct definition does
>> not give you any clue about how many elements the array has.

>
> Yes it does. There is one double, one char array, one struct
> (undefined), and one void* pointer. That makes 4 objects in the
> struct to me.

That is how many fields the *struct* has, NOT how many elements the
array has, which is what the OP asked. What makes you think iterating
over the array, as the OP stated was the requirement, and as I stated
above was the requirement, has anything to do with the number of fields
in the struct?

> If he wants to know how big the array of struct A is he will have
> to pass that value in. It is not calculable in the called
> function. That is a separate question.

It is the question asked and answered by various others. You seem to be
the only person reading it as a request for the number of fields, so I
suggest it is you expecting the OP to be asking the wrong question and
reading what you expect, not what is there.

--
Flash Gordon