Passing an Array of Struct's

Discuss PocketC for CE (including Desktop Edition)

Postby chrisleeuk on Wed Aug 21, 2002 11:02 am

I am new to Pocket C and don't have much experience in other forms of C.

What I want to do is set up a Struct and create an array of structs and then pass them into a function.

How do I do this?

I can get the following to work based on other forum posts:

struct strtime {int hour; int min;};

#define HOUR 0
#define MIN 1

tester(pointer pas) {

struct strtime mytest[10];
puts (mytest[0].hour);

This works returning '2' to the console.
However it only seems to pass the first element in the array and I cannot find a way to access any of the other elements from within the 'tester' function.

I am keen to pass structures around as I come from an Object Programming Language background and I find it much easier to work in this way.

If this is not possible can anyone suggest a work around?
Formulation, Visual Environment for Pocket C - (in development)
Posts: 31
Joined: Fri May 10, 2002 7:40 am

Postby Mickle on Wed Aug 21, 2002 1:51 pm

<pre id=code><font face=courier size=2 id=code>
struct strtime {
int hour;
int min;

#define HOUR 0
#define MIN 1

tester1(pointer pas, int num) //slow
pas[num * SIZEOF_STRTIME + HOUR] = 2;
pas[num * SIZEOF_STRTIME + MIN] = num;

tester2(pointer pas, int num) //faster (average: + 5*SIZEOF percents of speed)
for(pas = pas + (num - 1) * SIZEOF_STRTIME; num--; pas = pas - SIZEOF_STRTIME)
pas[HOUR] = 2;
pas[MIN] = num;

struct strtime mytest[10]; int i = 10;
//tester1(mytest, i);
tester2(mytest, i);
while(i--) putsl(mytest[i].hour +" "+ mytest[i].min);
</font id=code></pre id=code>
Posts: 48
Joined: Wed Apr 17, 2002 1:22 am
Location: Russia

Return to PocketC for CE

Who is online

Users browsing this forum: No registered users and 2 guests