We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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.
If the structs have identical data type, then it would be trivial to have an array of them.
If they haven't identical data type, then I really think you should consider a switch statement - how else will the code know what to do with a generic pointer that sometimes points to a struct of one type and at other times points to a struct of a different type?
how else will the code know what to do with a generic pointer that sometimes points to a struct of one type and at other times points to a struct of a different type? All the structures actually have character arrays where ascii values are stored. i have created structure & structure of structures (which contain the arrays) to organise the data which i can then access according to structures (groups).
And i want to create an array of pointers (i.e. address of memory locations). hence (according to me) compiler should only see it as an array of pointers to 1byte memory locations.
an array will make it easy to load the address into the pointer and start accessing the data from there.
i have a static struct variable having struct members and have to access this members on change of a count. Then you made a design mistake when you decided to put those data in a struct instead of an array. Time to refactor.
Hi Dhaval Solanki,
Would you please show some C source code, so that I can try it on my X86-PC compiler?
Then you made a design mistake Is it?
I used struct as i wanted to group the data. The data is basically strings, but the strings are grouped as per the objects they are related to. its like, _may be_ i am trying to achieve object oriented data management.
//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; PSetup PrdSet; PTimetbl PrdTymTbl[25]; PMd PrdMd[4]; }PDetail;
void dataprocessfunc(void) { ... PName *pnam = &LoadedData.PrdName; PSet *pstup = &LoadedData.PrdSet; PTimetbl *pmed = &LoadedData.PrdTymTbl[0]; ... UTILS_ClearBuf((uint8_t*)pnam, sizeof(LoadedData.PrdName)); psrc = uart_rxbuf; pdst = pnam->name; cntr = 0; while(cntr<8) { if(isalnum(*psrc) || *psrc == ' ' || *psrc == '_' || *psrc == '-' || *psrc == '.') if(*psrc != SEPERATOR && *psrc != ':' ) *pdst++ = *psrc; if(*psrc++ == SEPERATOR) { 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; UTILS_ClearBuf((uint8_t*)pstup, sizeof(LoadedData.PrdSet)); pdst = pstup->apparatus; while(cntr<15) { if(isalnum(*psrc) || *psrc == ' ' || *psrc == '_' || *psrc == '-' || *psrc == '.') { if(*psrc != SEPERATOR && *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++ == SEPERATOR) { 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; } } } //... contd in next post
Kindly dnt look deeper into the standardization _the variable name or function names may not be as per standards_
the whole function is not pasted. just the part of it so as to give a idea.
static PDetail LoadedData;
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; }
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.