This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

defining an array of pointers to structure members generates compiler error

espression must have a constant value

i have a static struct variable having struct members and have to access this members on change of a count. so instead of using a switch case statement to load respective struct member (address) into a pointer, i thought of defining a local array of pointers to point struct members.
but i get above error.
I tried to specifically define the struct at address 0xA0000000 of my parallel SDRAM using __attribute__ ((at(0xA0000000)))

how can this be achieved using an array of pointers??

PS: LPC1788 - but dont think the problem is architecture dependent.

Parents
  • Dear John,
    The its likely that the code will not work on a x86 architecture. Just compile successfully.

    You see the cases. i have been loading the pointer 'pdst' with structure members.
    i actually want an array with that has pointers to the members so that i can load the pointer in following manner:
    for eg: the array name is memarray.

    memarray[] = {
                    (*LoadedData.PrdName.name), (*LoadedData.PrdName.dos), (*LoadedData.PrdName.type),
                    (*LoadedData.PrdName.pen), (*LoadedData.PrdName.pure) //, ... and so on
                            };
    
    //so that i can load the pointers (address) directly in the same way as we load the array members
    
    pdst = memarray[cntr];  //
    

    loading the pointers directly into the pdst pointer and start accessing the data - is the idea. This will reduce the number of cases (the if-else statements).

Reply
  • Dear John,
    The its likely that the code will not work on a x86 architecture. Just compile successfully.

    You see the cases. i have been loading the pointer 'pdst' with structure members.
    i actually want an array with that has pointers to the members so that i can load the pointer in following manner:
    for eg: the array name is memarray.

    memarray[] = {
                    (*LoadedData.PrdName.name), (*LoadedData.PrdName.dos), (*LoadedData.PrdName.type),
                    (*LoadedData.PrdName.pen), (*LoadedData.PrdName.pure) //, ... and so on
                            };
    
    //so that i can load the pointers (address) directly in the same way as we load the array members
    
    pdst = memarray[cntr];  //
    

    loading the pointers directly into the pdst pointer and start accessing the data - is the idea. This will reduce the number of cases (the if-else statements).

Children
  • #include <stdio.h>
    #include <stdint.h>
    
    
    typedef struct {
        uint8_t A1[10];
        uint8_t A2[20];
        uint8_t A3[30];
    } Type_StA;
    
    typedef struct {
        uint8_t B1[10];
        uint8_t B2[20];
        uint8_t B3[30];
        Type_StA PhStA;
    } Type_StB;
    
    Type_StB The_Struct;
    
    uint8_t * Array[] = { The_Struct.B1, The_Struct.B2, The_Struct.B3, The_Struct.PhStA.A3 };
    
    
    int main(void)
    {
        The_Struct.B2[0] = '_';
        The_Struct.B2[1] = 'B';
        The_Struct.B2[2] = '2';
        The_Struct.B2[3] = 0;
    
        The_Struct.PhStA.A3[0] = '_';
        The_Struct.PhStA.A3[1] = 'A';
        The_Struct.PhStA.A3[2] = '3';
        The_Struct.PhStA.A3[3] = 0;
    
        printf("%s\n", Array[1] );
        printf("%s\n", Array[3] );
    
        return 0;
    }
    
    
    

  • Thank you John Linq. Thanks a lot!
    That was highly appreciable.