Skip to main content
WOGoos
Associate III
August 8, 2020
Question

How to map a array of types over a struct with a sequence/list of the same types.

  • August 8, 2020
  • 5 replies
  • 1805 views

Hi guys, Hope some one can help me with this

I have defined an alarm struct of repeating struct memers that must manage various alarm massages and alarm audio signals.

I want to address these alarm's in the following way, for example

1) alarm.spray.stat

but also as

2) alarmlist[0].stat;

Both the struct and the array are in fact arrays of repeating alarm_t types and it should be possible to map them over eacht other having the same start address

For this I need to assign the alarm struct address to the alarmlist[] array address.

Tricky i know but on a 8bit controller like the ATmega 328 it should not be a problem. I also want to use this technique on STM32 controllers. How can i do this with out having conflicts at compile time.

I did try this :

alarmlist=(alarm_t)&alarm but that gave me a error.

typedef struct alarm_t {

char * msg;

uint8_t prio : 4;

uint8_t stat : 4;

uint8_t Buzzpattern;

uint8_t duration : 4;

uint8_t intermit : 4;

} alarm_t;

struct alarm_state_t {

alarm_t spray;

alarm_t stall;

alarm_t empty;

} alarm = { { },{ },{ } };

alarm_t alarmlist[];

    This topic has been closed for replies.

    5 replies

    TDK
    August 8, 2020

    alarm_t * alarmlist = alarm;

    Actually this is a bit trickier since the struct could be padded.

    This works but is probably compiler dependent:

    typedef struct alarm_t {
     char * msg;
     uint8_t prio : 4;
     uint8_t stat : 4;
     uint8_t Buzzpattern;
     uint8_t duration : 4;
     uint8_t intermit : 4;
    } alarm_t;
     
    struct alarm_state_t {
     alarm_t spray;
     alarm_t stall;
     alarm_t empty;
    } alarm = { { },{ },{ } };
     
    alarm_t * alarmlist = (alarm_t *) &alarm.spray;
     
     
    void main() {
     
     ASSERT_EQ((uint32_t) &alarmlist[0], (uint32_t) &alarm.spray);
     ASSERT_EQ((uint32_t) &alarmlist[1], (uint32_t) &alarm.stall);
     ASSERT_EQ((uint32_t) &alarmlist[2], (uint32_t) &alarm.empty);
     
    }

    "If you feel a post has answered your question, please click ""Accept as Solution""."
    Piranha
    Principal III
    August 9, 2020

    The requirement is strange...

    I would drop the structure thing and use an array with named constants for indexes:

    enum alarm {
    	ALARM_SPRAY,
    	ALARM_STALL,
    	ALARM_EMPTY,
     
    	ALARM_NUMBER
    };
     
    alarm_t alarmlist[ALARM_NUMBER];
     
    alarmlist[ALARM_SPRAY].stat = 1;

    WOGoos
    WOGoosAuthor
    Associate III
    August 10, 2020

    Thanks Piranha, I agree that is possibility too and I will take it in considderation. however it's not an answer to my question. How to map an array of structs on a struct with a series of equal structs as used for the array

    RMcCa
    Senior II
    August 9, 2020

    Pointers?

    One of the great things about 32bit arm programming is how everything on the chip is accessible thru a flat 32bit address space. With C, what that means is there is no real difference between a pointer and a uint32_t. You are free to manipulate the pointers (unsigned math & sizeof) and cast and reference/dereference as you need.​

    WOGoos
    WOGoosAuthor
    Associate III
    August 10, 2020

    Sorry RMcCa but I think you missed the point, but thanks for responding