Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. structs

structs

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anders 1
    wrote on last edited by
    #1

    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:

    M J R D 4 Replies Last reply
    0
    • A Anders 1

      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:

      M Offline
      M Offline
      markkuk
      wrote on last edited by
      #2

      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.

      A 1 Reply Last reply
      0
      • A Anders 1

        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:

        J Offline
        J Offline
        Jorgen Sigvardsson
        wrote on last edited by
        #3

        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.

        A 1 Reply Last reply
        0
        • M markkuk

          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.

          A Offline
          A Offline
          Anders 1
          wrote on last edited by
          #4

          i dont need to use the array syntax, i just need to access the members of the struct without using the names

          I 1 Reply Last reply
          0
          • A Anders 1

            i dont need to use the array syntax, i just need to access the members of the struct without using the names

            I Offline
            I Offline
            Ian Darling
            wrote on last edited by
            #5

            [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

            A 1 Reply Last reply
            0
            • I Ian Darling

              [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

              A Offline
              A Offline
              Anders 1
              wrote on last edited by
              #6

              yeah but I tought there was some fancy way of doing it with a ptr like this char* text=pS+(sizeof(char*)*i);

              I 1 Reply Last reply
              0
              • A Anders 1

                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:

                R Offline
                R Offline
                Rickard Andersson20
                wrote on last edited by
                #7

                Overload the [] operator? Rickard Andersson Here is my card, contact me later! UIN: 50302279 Sonork: 37318

                1 Reply Last reply
                0
                • J Jorgen Sigvardsson

                  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.

                  A Offline
                  A Offline
                  Anders 1
                  wrote on last edited by
                  #8

                  i think i have gotten it to work now, thanx (and yes i needed something nasty)

                  1 Reply Last reply
                  0
                  • A Anders 1

                    yeah but I tought there was some fancy way of doing it with a ptr like this char* text=pS+(sizeof(char*)*i);

                    I Offline
                    I Offline
                    Ian Darling
                    wrote on last edited by
                    #9

                    [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

                    1 Reply Last reply
                    0
                    • A Anders 1

                      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:

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      [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.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups