473,395 Members | 1,411 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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?
Jun 27 '08 #1
4 6887
uidzer0 <be************@gmail.comwrote:
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
Jun 27 '08 #2
On Apr 15, 9:13*am, r...@hoekstra-uitgeverij.nl (Richard Bos) wrote:
uidzer0 <ben.lemasur...@gmail.comwrote:
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.
Jun 27 '08 #3
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 **
Jun 27 '08 #4
On Tue, 15 Apr 2008 16:59:26 -0400, CBFalconer <cb********@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
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jon | last post by:
Hi all, I am trying to dynamically fill an array with any unique values from a field (objRS.Fields(5)) in my database with the following code below. However it errors with "The array is fixed...
14
by: Peter Olcott | last post by:
I want to be able to efficiently build data structures at run-time. These data structures need to be accessed with minimal time. The only a few ways that come immediately to mind would be some...
4
by: Stone Chen | last post by:
Hello, I have form that uses javascript createElement to add additional input fields to it. However, my validating script will not process new input fields because it can only find the named...
11
by: skumar434 | last post by:
Hi everybody, I am faceing problem while assigning the memory dynamically to a array of structures . Suppose I have a structure typedef struct hom_id{ int32_t nod_de; int32_t hom_id;
1
by: vj | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file? I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
0
by: vijendra | last post by:
How i can populate all fileds dynamically in jsp page based on contents found in xml file?I have written jsp servlets and java class file. i transferred automatic data from jsp to servlet then to...
4
by: assgar | last post by:
Hi I am stuck on a problem. I use 3 scripts(form, function and process). Development on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. The form displays...
3
by: Jinkuzo | last post by:
Have been working on this project for a week, unfortunately missed the lesson on it and have been struggling to figure it out. These are the instructions and the point at which I'm up to. ...
26
by: tesh.uk | last post by:
Hi Gurus, I have written the following code with the help of Ivor Horton's Beginning C : // Structures, Arrays of Structures. #include "stdafx.h" #include "stdio.h" #define MY_ARRAY 15
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.