size of an array (number of elements)

G

Grey Alien

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
}
 
H

Harald van =?UTF-8?B?RMSzaw==?=

Grey said:
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.
 
W

Walter Roberson

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

Flash Gordon

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

CBFalconer

Grey said:
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.
 
F

Flash Gordon

CBFalconer wrote, On 05/07/07 00:53:
Grey said:
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.
 
K

Keith Thompson

CBFalconer said:
Grey said:
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.
 
C

CBFalconer

Flash said:
CBFalconer wrote, On 05/07/07 00:53:
Grey said:
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.
 
K

Keith Thompson

CBFalconer said:
Flash said:
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."
 
F

Flash Gordon

CBFalconer wrote, On 05/07/07 07:32:
Flash said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top