Help needed to solve a pointer problem.

Dear Reader,
I'm having a pointer problem. I work within a program with an array of structs and these structs have various struct members. Some were in my program I need to access one of the the struct members with in this array by a pointer. My question is can some one show me how to do this.

Some part of my program are the following

struct pir_struct {
byte index ; // Index to the array of port structs.
byte state ; // State of the PIR statemachine (PIR passive infra-red sensor)
unsigned long movtime, movdelta ; // Movement detected state machine time variables
unsigned long nmvtime, nmvdelta ; // No-movement detected state machine time variables
byte PIRval ; // last read momentary value of the PIR
};

struct relay_struct{
byte index ; // Index to the array of port structs.
byte state ; // State of the relay which is on of off
unsigned long ontime, ondelta ; // Relay-on state machine time variables
unsigned long offtime,offdelta ; // Relay-off state machine time variables
byte *ptr2state ; // ptr to PIR struct->state element
};

pir pirlist[] = {{0, NMOV, 0,0 ,0,0,0}, // pirlist[] is an array or pir_stucts.
{2, NMOV, 0,0 ,0,0,0},
{3, MOV, 0,5000 ,0,0,0}};

relay relaylist[] = {{0, OFF, DUMMY,DUMMY ,0 ,0 ,0}, // Arraylist[] is an array or relay_stucts.
{4, OFF,0,5000,0,0, (byte*)&(pirlist[1]->state) },
{5, OFF,0,0000,0,0, (byte*)&(pirlist[2]->state) },
{6, OFF,0,0000,0,0, (byte*)&(termlist[1]->state) }};

I see that the text doesn't apear as is does in the editor my excuser for that.

I try to initialise the relaylist[1].ptr2state element which is a pointer with the address of pirlist[1].state.
According to what I know about pointers this can be done with the value : &(pirlist[1]->state) But Im not sure if this address really points to the element pirlist[1].state.

Can some one help me with this.

regards Oscar

Firstly you can go and add code tags to you posting to

// improve the formatting

Alas the recent forum changes replaced the easy to spot icon for code tags with a
dismally awful one that cannot easily be spotted - its blue, sort of scroll
thing with a <> across it (blue on a white background is the least legible colour
combination, yuk - bring back the # button please)

Right, back to the issue.

You don't have a pointer to a struct, you have a pointer to byte, so that needs fixing.
A pointer to struct pir_struct is declared thus

  struct pir_struct * ptr;

The address of an element of an array is just &a[n] or equivalently a+n,
but the latter is an example of pointer arithmetic (arrays are always usable as pointers),
and not really good style.

So something like:

  struct pir_struct * ptr = &pirlist[n] ;

However on a memory-constrained microcontroller its usually simpler and less wasteful
of resources to use a byte-sized index into the array, not a pointer, since you don't have
the RAM for more than 255 array entries anyway, and pointers are 2 or 3 bytes.

  byte pir_index = n ;

If all your structs are in an array (rather than being malloc'd and free'd) then indexing
is usually simplest and clearest (and more debuggable).

and pointers are 2 or 3 bytes.

Pointers are ints, and they are always two bytes on the Arduino.

64 bit operating systems are named because pointers on those OSes are 64 bits (8 bytes), because they need to be able to point to more than 2 to the 32nd bytes of memory.

@PaulS:

Pointers are ints...

Really? It's okay to say they take two bytes, but saying they are ints is misleading.

It's okay to say they take two bytes, but saying they are ints is misleading.

Well, they're not floats. A pointer is an address. An address is an int.

Well, they're not floats. A pointer is an address. An address is an int.

So, I can then take two pointers and divide (or multiply) one by the other and get something meaningful from it? After all, all ints support integer math. Given that a valid pointer can only have two things in it: a memory address or null, what is a "null int"? Also, if I do

x++;

on an int, it increases its value by 1. If I do the same thing on a pointer to long, it increases its value by 4. Pointers are scaled by their type specifier, ints are not.

Sorry, Paul, but I still think your statement is misleading at best and simply wrong at the extreme.