Dynamically loop through array of structures

Discussion in 'C Programming' started by uidzer0, Apr 15, 2008.

  1. uidzer0

    uidzer0 Guest

    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?
    uidzer0, Apr 15, 2008
    #1
    1. Advertising

  2. uidzer0

    Richard Bos Guest

    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.


    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
    Richard Bos, Apr 15, 2008
    #2
    1. Advertising

  3. uidzer0

    uidzer0 Guest

    On Apr 15, 9:13 am, (Richard Bos) 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.

    >
    > 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.
    uidzer0, Apr 15, 2008
    #3
  4. On Tue, 15 Apr 2008 16:59:26 -0400, CBFalconer <>
    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
    David Thompson, Apr 28, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alfonso Morra
    Replies:
    11
    Views:
    710
    Emmanuel Delahaye
    Sep 24, 2005
  2. Replies:
    11
    Views:
    507
    Barry Schwarz
    Aug 15, 2006
  3. Roedy Green
    Replies:
    3
    Views:
    423
    Mike Schilling
    Sep 13, 2008
  4. Isaac Won
    Replies:
    9
    Views:
    364
    Ulrich Eckhardt
    Mar 4, 2013
  5. DSF
    Replies:
    9
    Views:
    202
    Eric Sosman
    Sep 9, 2013
Loading...

Share This Page