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
asking about the number of elements in an array of structures.

--
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.

Reread the underlined bits.
--
Flash Gordon

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Jason Cavett Java 5 05-25-2008 08:32 AM Frank ess Digital Photography 0 11-14-2006 05:08 PM eagle_jyjh@citiz.net C++ 8 04-10-2006 03:05 PM eagle_jyjh@citiz.net C Programming 5 04-09-2006 02:49 PM Zygmunt Krynicki C++ 1 10-11-2003 01:18 AM

Advertisments