structs
-
say i have a struct like this:
typedef struct { char* text1; char* text2; ... char* text25; }MYS,*PMYS;
how can i access the struct like this?PMYS pS=SomehowGetPtrToStruct(); for (int i=0; i < MYS_TEXTCOUNT; i++) { char* text=pS[i]; dosomething(text); }
i dont want to get the items like this:char* text=pS->text2;
i also tried to do stuff like:char* text=pS+(sizeof(char*)*i);
but i cant get it to work:confused: -
say i have a struct like this:
typedef struct { char* text1; char* text2; ... char* text25; }MYS,*PMYS;
how can i access the struct like this?PMYS pS=SomehowGetPtrToStruct(); for (int i=0; i < MYS_TEXTCOUNT; i++) { char* text=pS[i]; dosomething(text); }
i dont want to get the items like this:char* text=pS->text2;
i also tried to do stuff like:char* text=pS+(sizeof(char*)*i);
but i cant get it to work:confused: -
say i have a struct like this:
typedef struct { char* text1; char* text2; ... char* text25; }MYS,*PMYS;
how can i access the struct like this?PMYS pS=SomehowGetPtrToStruct(); for (int i=0; i < MYS_TEXTCOUNT; i++) { char* text=pS[i]; dosomething(text); }
i dont want to get the items like this:char* text=pS->text2;
i also tried to do stuff like:char* text=pS+(sizeof(char*)*i);
but i cant get it to work:confused:if you really want to be nasty, you have to recast the pS pointer as a char**.
char** p = reinterpret_cast<char**>(pS)
Now p[0] points to text1, p[1] to text2, etc. However, this may be totally nonportable. If this is to work, you have to be sure that text1 is aligned with the beginning of the struct. Also, if sizeof(char*) is not the same size as the member alignment "distance", you will run into trouble. May I suggest a totally different structure? How about using std::vectorstd::string or are you not in control of the layout of this structure and associated functions? Messing with raw pointers like that is highly error prone and will most likely give rise to late debugging session... -- Seraphim Shock. Gold for your ears. -
If you want to use the array syntax, use an array (or a
std::vector
). You could overload the[]
operator for your struct if you want to obfuscate your code. -
i dont need to use the array syntax, i just need to access the members of the struct without using the names
[Anders] wrote: i dont need to use the array syntax, i just need to access the members of the struct without using the names But that's exactly what you want to do :confused:
ps[i]
= use of array syntax -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky -
[Anders] wrote: i dont need to use the array syntax, i just need to access the members of the struct without using the names But that's exactly what you want to do :confused:
ps[i]
= use of array syntax -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky -
say i have a struct like this:
typedef struct { char* text1; char* text2; ... char* text25; }MYS,*PMYS;
how can i access the struct like this?PMYS pS=SomehowGetPtrToStruct(); for (int i=0; i < MYS_TEXTCOUNT; i++) { char* text=pS[i]; dosomething(text); }
i dont want to get the items like this:char* text=pS->text2;
i also tried to do stuff like:char* text=pS+(sizeof(char*)*i);
but i cant get it to work:confused:Overload the
[]
operator? Rickard Andersson Here is my card, contact me later! UIN: 50302279 Sonork: 37318 -
if you really want to be nasty, you have to recast the pS pointer as a char**.
char** p = reinterpret_cast<char**>(pS)
Now p[0] points to text1, p[1] to text2, etc. However, this may be totally nonportable. If this is to work, you have to be sure that text1 is aligned with the beginning of the struct. Also, if sizeof(char*) is not the same size as the member alignment "distance", you will run into trouble. May I suggest a totally different structure? How about using std::vectorstd::string or are you not in control of the layout of this structure and associated functions? Messing with raw pointers like that is highly error prone and will most likely give rise to late debugging session... -- Seraphim Shock. Gold for your ears. -
yeah but I tought there was some fancy way of doing it with a ptr like this char* text=pS+(sizeof(char*)*i);
[Anders] wrote: yeah but I tought there was some fancy way of doing it with a ptr like this char* text=pS+(sizeof(char*)*i); No, because all you're doing is adding
sizeof(char) * i
to the pointer value of pS, which would be a really messed up way of accessing an array of your MYS struct. If you really want to do stuff like that, you have to learn exactly what + does in the context of a pointer. -- Ian Darling "The moral of the story is that with a contrived example, you can prove anything." - Joel Spolsky -
say i have a struct like this:
typedef struct { char* text1; char* text2; ... char* text25; }MYS,*PMYS;
how can i access the struct like this?PMYS pS=SomehowGetPtrToStruct(); for (int i=0; i < MYS_TEXTCOUNT; i++) { char* text=pS[i]; dosomething(text); }
i dont want to get the items like this:char* text=pS->text2;
i also tried to do stuff like:char* text=pS+(sizeof(char*)*i);
but i cant get it to work:confused:[Anders] wrote: i dont want to get the items like this:char* text=pS->text2; There's bound to be a reason why you don't want to use this syntax. Any other way would be hard to read and maintain, and probably wouldn't be portable.
A rich person is not the one who has the most, but the one that needs the least.