Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Dynamically loop through array of structures

Reply
Thread Tools

Dynamically loop through array of structures

 
 
uidzer0
Guest
Posts: n/a
 
      04-15-2008
Hey everyone,

I apologize in advance for this novice question however I'm not having
any luck finding the answer myself.

I'm attempting to loop through an array of structures passed to a
function, however I'm not sure how to obtain the number of elements in
the array.

The structure is defined as:
typedef struct {
time_t creation_date;
int priority;
char *text;
} note;

And here's the function prototype:
void get_notes(note *notes);

How would I dynamically loop through *notes?
 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      04-15-2008
uidzer0 <(E-Mail Removed)> wrote:

> I'm attempting to loop through an array of structures passed to a
> function, however I'm not sure how to obtain the number of elements in
> the array.


You can't. You have to pass that number in to the function, use a
terminator element, or have some other way to find out. This is the same
for all arrays, whether or not they're made of structs.

Richard
 
Reply With Quote
 
 
 
 
uidzer0
Guest
Posts: n/a
 
      04-15-2008
On Apr 15, 9:13*am, (E-Mail Removed) (Richard Bos) wrote:
> uidzer0 <(E-Mail Removed)> wrote:
> > I'm attempting to loop through an array of structures passed to a
> > function, however I'm not sure how to obtain the number of elements in
> > the array.

>
> You can't. You have to pass that number in to the function, use a
> terminator element, or have some other way to find out. This is the same
> for all arrays, whether or not they're made of structs.
>
> Richard


Ahh... well that makes more sense - thanks.
 
Reply With Quote
 
David Thompson
Guest
Posts: n/a
 
      04-28-2008
On Tue, 15 Apr 2008 16:59:26 -0400, CBFalconer <(E-Mail Removed)>
wrote:

> uidzer0 wrote:
> >
> > I'm attempting to loop through an array of structures passed to
> > a function, however I'm not sure how to obtain the number of
> > elements in the array.
> >
> > The structure is defined as:
> > typedef struct {
> > time_t creation_date;
> > int priority;
> > char *text;
> > } note;
> >
> > And here's the function prototype:
> > void get_notes(note *notes);
> >
> > How would I dynamically loop through *notes?

>
> One way is to hold the pointers to note (i.e. the note* items) in
> an array terminated with a NULL. Then the function might be:
>

OP doesn't have an array of pointers, but one pointer to an array.

> void get_notes(note *notes) {
> while (*notes) {
> process_one_note(*notes);
> notes++;
> }
> }
>

For an array of pointer you need (modulo spacing)
void get_notes (note * * notes)
then as you have it. Or (equivalent to the compiler)
void get_notes (note * notes [] )
to emphasize the array-of-pointer-ness, but downplay the fact that
array parameters/arguments are really pointers; pick your poison.

- formerly david.thompson1 || achar(64) || worldnet.att.net
 
Reply With Quote
 
 
 
Reply

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 Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Re: How to loop through a list while inside the loop, the list size may be decreased? Roedy Green Java 3 09-13-2008 01:51 AM
How to assign memory dynamically to a array of structures skumar434@gmail.com C Programming 11 08-15-2006 01:52 AM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
Type Casting IPv4 and IPv6 structures to Generic Structures tweak C Programming 14 06-11-2004 02:43 PM



Advertisments