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
  • Source code seems not usable, can not see the problem.

    The below code compiled successfully.

    #include <stdio.h>
    #include <stdint.h>
    
    #define NAME_SIZE 12
    #define PROTO_NAME_SIZE 12
    
    //Definitions & declarations
    
    typedef struct TimeFormat
    {
            uint8_t hrs[4];
            uint8_t mins[2];
            uint8_t secs[2];
    }AsciiTime;
    
    typedef struct ProtoDetail
    {
            uint8_t name[NAME_SIZE];
            uint8_t dos[NAME_SIZE];
            uint8_t type[NAME_SIZE];
            uint8_t pen[5];
            uint8_t pure[5];
    }PName;
    
    typedef struct SetDetail
    {
            uint8_t apparatus[NAME_SIZE];
            uint8_t temp[4];
            uint8_t pwrfaildelay[2];
            AsciiTime sync;
            AsciiTime mnfldtym;
            AsciiTime dwntym;
            AsciiTime haltduratn;
    }PSet;
    
    typedef struct TimeTableDetail
    {
            uint8_t sampleno[2];
            AsciiTime stym;
            uint8_t rpm[3];
            uint8_t vol[4];
            uint8_t action[35];
    }PTimetbl;
    
    typedef struct MDetails
    {
            uint8_t msno[2];
            uint8_t mname[PROTO_NAME_SIZE];
            uint8_t phvalue[4];
            uint8_t volume[4];
    }PMd;
    
    typedef struct ProDetail
    {
            PName           PrdName;
            PSet            PrdSet;
            PTimetbl        PrdTymTbl[25];
            PMd                     PrdMd[4];
    }PDetail;
    
    static PDetail LoadedData;
    
    void dataprocessfunc(void)
    {
            PName *pnam = &LoadedData.PrdName;
            PSet *pstup = &LoadedData.PrdSet;
            PTimetbl *pmed = &LoadedData.PrdTymTbl[0];
            uint8_t * pdst;
            uint8_t * psrc;
            int cntr, maxlen, semicol;
    
            pdst = pnam->name;
            cntr = 0;
            while(cntr<8)
            {
                    if(isalnum(*psrc) || *psrc == ' ' || *psrc == '_' || *psrc == '-' || *psrc == '.')
                            if(*psrc != 0x1010 && *psrc != ':' )
                                    *pdst++ = *psrc;
    
    
    
    
                    if(*psrc++ == 0x1010)
                    {
                            cntr++;
    
    
    
    
                            switch (cntr)
                            {
                                    case 3:
                                            pdst = pnam->name;
                                            maxlen = sizeof(pnam->name);
                                    break;
    
    
    
    
                                    case 4:
                                            pdst = pnam->dos;
                                            maxlen = sizeof(pnam->dos);
                                    break;
    
    
    
    
                                    case 5:
                                            pdst = pnam->type;
                                            maxlen = sizeof(pnam->type);
                                    break;
    
    
    
    
                                    case 6:
                                            pdst = pnam->pen;
                                            maxlen = sizeof(pnam->pen);
                                    break;
    
    
    
    
                                    case 7:
                                            pdst = pnam->pure;
                                            maxlen = sizeof(pnam->pure);
                                    break;
                            }
                    }
            }
    
    
    
    
            semicol = 0;
            pdst = pstup->apparatus;
            while(cntr<15)
            {
                    if(isalnum(*psrc) || *psrc == ' ' || *psrc == '_' || *psrc == '-' || *psrc == '.')
                    {
                            if(*psrc != 0x1010 && *psrc != ':' )
                                    *pdst++ = *psrc;
                    }
    
    
    
    
                    if(cntr==11 && *psrc == ':')
                            pdst = pstup->sync.secs;
    
    
    
    
                    if(cntr==12 && *psrc == ':')
                            pdst = pstup->mnfldtym.secs;
    
    
    
    
                    if(cntr==13 && *psrc == ':')
                            pdst = pstup->dwntym.secs;
    
    
    
    
                    if(cntr==14 && *psrc == ':')
                    {
                            semicol++;
                            if(semicol==2)
                            {
                                    pdst = pstup->haltduratn.secs;
                            }
                            else if(semicol==1)
                            {
                                    pdst = pstup->haltduratn.mins;
                            }
                    }
    
    
    
    
                    if(*psrc++ == 0x1010)
                    {
                            cntr++;
    
    
    
    
                            switch (cntr)
                            {
                                    case 8:
                                            pdst = pstup->apparatus;
                                            maxlen = sizeof(pstup->apparatus);
                                    break;
    
    
    
    
                                    case 9:
                                            pdst = pstup->temp;
                                            maxlen = sizeof(pstup->temp);
                                    break;
    
    
    
    
                                    case 10:
                                            pdst = pstup->pwrfaildelay;
                                            maxlen = sizeof(pstup->pwrfaildelay);
                                    break;
    
    
    
    
                                    case 11:
                                            pdst = pstup->sync.mins;
                                            maxlen = sizeof(pstup->sync);
                                    break;
    
    
    
    
                                    case 12:
                                            pdst = pstup->mnfldtym.mins;
                                            maxlen = sizeof(pstup->mnfldtym);
                                    break;
    
    
    
    
                                    case 13:
                                            pdst = pstup->dwntym.mins;
                                            maxlen = sizeof(pstup->dwntym);
                                    break;
    
    
    
    
                                    case 14:
                                            pdst = pstup->haltduratn.hrs;
                                            *pdst++ = ' ';
                                            *pdst++ = ' ';
                                            maxlen = sizeof(pstup->haltduratn);
                                    break;
                            }
                    }
            }
    }
    
    int main(void)
    {
        dataprocessfunc();
        return 0;
    }
    
    
    

Reply
  • Source code seems not usable, can not see the problem.

    The below code compiled successfully.

    #include <stdio.h>
    #include <stdint.h>
    
    #define NAME_SIZE 12
    #define PROTO_NAME_SIZE 12
    
    //Definitions & declarations
    
    typedef struct TimeFormat
    {
            uint8_t hrs[4];
            uint8_t mins[2];
            uint8_t secs[2];
    }AsciiTime;
    
    typedef struct ProtoDetail
    {
            uint8_t name[NAME_SIZE];
            uint8_t dos[NAME_SIZE];
            uint8_t type[NAME_SIZE];
            uint8_t pen[5];
            uint8_t pure[5];
    }PName;
    
    typedef struct SetDetail
    {
            uint8_t apparatus[NAME_SIZE];
            uint8_t temp[4];
            uint8_t pwrfaildelay[2];
            AsciiTime sync;
            AsciiTime mnfldtym;
            AsciiTime dwntym;
            AsciiTime haltduratn;
    }PSet;
    
    typedef struct TimeTableDetail
    {
            uint8_t sampleno[2];
            AsciiTime stym;
            uint8_t rpm[3];
            uint8_t vol[4];
            uint8_t action[35];
    }PTimetbl;
    
    typedef struct MDetails
    {
            uint8_t msno[2];
            uint8_t mname[PROTO_NAME_SIZE];
            uint8_t phvalue[4];
            uint8_t volume[4];
    }PMd;
    
    typedef struct ProDetail
    {
            PName           PrdName;
            PSet            PrdSet;
            PTimetbl        PrdTymTbl[25];
            PMd                     PrdMd[4];
    }PDetail;
    
    static PDetail LoadedData;
    
    void dataprocessfunc(void)
    {
            PName *pnam = &LoadedData.PrdName;
            PSet *pstup = &LoadedData.PrdSet;
            PTimetbl *pmed = &LoadedData.PrdTymTbl[0];
            uint8_t * pdst;
            uint8_t * psrc;
            int cntr, maxlen, semicol;
    
            pdst = pnam->name;
            cntr = 0;
            while(cntr<8)
            {
                    if(isalnum(*psrc) || *psrc == ' ' || *psrc == '_' || *psrc == '-' || *psrc == '.')
                            if(*psrc != 0x1010 && *psrc != ':' )
                                    *pdst++ = *psrc;
    
    
    
    
                    if(*psrc++ == 0x1010)
                    {
                            cntr++;
    
    
    
    
                            switch (cntr)
                            {
                                    case 3:
                                            pdst = pnam->name;
                                            maxlen = sizeof(pnam->name);
                                    break;
    
    
    
    
                                    case 4:
                                            pdst = pnam->dos;
                                            maxlen = sizeof(pnam->dos);
                                    break;
    
    
    
    
                                    case 5:
                                            pdst = pnam->type;
                                            maxlen = sizeof(pnam->type);
                                    break;
    
    
    
    
                                    case 6:
                                            pdst = pnam->pen;
                                            maxlen = sizeof(pnam->pen);
                                    break;
    
    
    
    
                                    case 7:
                                            pdst = pnam->pure;
                                            maxlen = sizeof(pnam->pure);
                                    break;
                            }
                    }
            }
    
    
    
    
            semicol = 0;
            pdst = pstup->apparatus;
            while(cntr<15)
            {
                    if(isalnum(*psrc) || *psrc == ' ' || *psrc == '_' || *psrc == '-' || *psrc == '.')
                    {
                            if(*psrc != 0x1010 && *psrc != ':' )
                                    *pdst++ = *psrc;
                    }
    
    
    
    
                    if(cntr==11 && *psrc == ':')
                            pdst = pstup->sync.secs;
    
    
    
    
                    if(cntr==12 && *psrc == ':')
                            pdst = pstup->mnfldtym.secs;
    
    
    
    
                    if(cntr==13 && *psrc == ':')
                            pdst = pstup->dwntym.secs;
    
    
    
    
                    if(cntr==14 && *psrc == ':')
                    {
                            semicol++;
                            if(semicol==2)
                            {
                                    pdst = pstup->haltduratn.secs;
                            }
                            else if(semicol==1)
                            {
                                    pdst = pstup->haltduratn.mins;
                            }
                    }
    
    
    
    
                    if(*psrc++ == 0x1010)
                    {
                            cntr++;
    
    
    
    
                            switch (cntr)
                            {
                                    case 8:
                                            pdst = pstup->apparatus;
                                            maxlen = sizeof(pstup->apparatus);
                                    break;
    
    
    
    
                                    case 9:
                                            pdst = pstup->temp;
                                            maxlen = sizeof(pstup->temp);
                                    break;
    
    
    
    
                                    case 10:
                                            pdst = pstup->pwrfaildelay;
                                            maxlen = sizeof(pstup->pwrfaildelay);
                                    break;
    
    
    
    
                                    case 11:
                                            pdst = pstup->sync.mins;
                                            maxlen = sizeof(pstup->sync);
                                    break;
    
    
    
    
                                    case 12:
                                            pdst = pstup->mnfldtym.mins;
                                            maxlen = sizeof(pstup->mnfldtym);
                                    break;
    
    
    
    
                                    case 13:
                                            pdst = pstup->dwntym.mins;
                                            maxlen = sizeof(pstup->dwntym);
                                    break;
    
    
    
    
                                    case 14:
                                            pdst = pstup->haltduratn.hrs;
                                            *pdst++ = ' ';
                                            *pdst++ = ' ';
                                            maxlen = sizeof(pstup->haltduratn);
                                    break;
                            }
                    }
            }
    }
    
    int main(void)
    {
        dataprocessfunc();
        return 0;
    }
    
    
    

Children
  • So where, exactly, did you get the error?

  • 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).

  • the code with the switch-case statement is working absolutely fine. (sorry john for wasting your time. may be i fell short of explaining you clearly).
    but the errors are generated with the array of pointers.

  • #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.