# size of an array (number of elements)

Discussion in 'C Programming' started by Grey Alien, Jul 4, 2007.

1. ### Grey AlienGuest

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
}

Grey Alien, Jul 4, 2007

2. ### Harald van =?UTF-8?B?RMSzaw==?=Guest

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.

Harald van =?UTF-8?B?RMSzaw==?=, Jul 4, 2007

3. ### Walter RobersonGuest

In article <>,
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 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

Walter Roberson, Jul 4, 2007
4. ### Flash GordonGuest

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

Flash Gordon, Jul 4, 2007
5. ### CBFalconerGuest

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

CBFalconer, Jul 5, 2007
6. ### Flash GordonGuest

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

Flash Gordon, Jul 5, 2007
7. ### Keith ThompsonGuest

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

Keith Thompson, Jul 5, 2007
8. ### CBFalconerGuest

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

CBFalconer, Jul 5, 2007
9. ### Keith ThompsonGuest

CBFalconer <> 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) <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"

Keith Thompson, Jul 5, 2007
10. ### Flash GordonGuest

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.