PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Dynamically loop through array of structures
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Dynamically loop through array of structures

Réponse
 
LinkBack Outils de la discussion
Vieux 15/04/2008, 15h11   #1
uidzer0
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Dynamically loop through array of structures

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?
  Réponse avec citation
Vieux 15/04/2008, 15h13   #2
Richard Bos
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Dynamically loop through array of structures

uidzer0 <ben.lemasurier@gmail.com> 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
  Réponse avec citation
Vieux 15/04/2008, 15h19   #3
uidzer0
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Dynamically loop through array of structures

On Apr 15, 9:13am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
> uidzer0 <ben.lemasur...@gmail.com> 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.
  Réponse avec citation
Vieux 15/04/2008, 21h59   #4
CBFalconer
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Dynamically loop through array of structures

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:

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

but you would be better advised to get rid of the void and return
an error indicator. Same for process_one_note()

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.


** Posted from http://www.teranews.com **
  Réponse avec citation
Vieux 28/04/2008, 04h36   #5
David Thompson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Dynamically loop through array of structures

On Tue, 15 Apr 2008 16:59:26 -0400, CBFalconer <cbfalconer@yahoo.com>
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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h45.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,13427 seconds with 13 queries