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. Simple question

Simple question

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
7 Posts 5 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.
  • M Offline
    M Offline
    Mathias S
    wrote on last edited by
    #1

    struct Data { int x; int y; }; Data* pData = new Data[30]; how do I access value x in item 5 in the Data array pData[5]->x = 3; // err pData[5].x = 3; //err /Mathias

    V J 2 Replies Last reply
    0
    • M Mathias S

      struct Data { int x; int y; }; Data* pData = new Data[30]; how do I access value x in item 5 in the Data array pData[5]->x = 3; // err pData[5].x = 3; //err /Mathias

      V Offline
      V Offline
      valikac
      wrote on last edited by
      #2

      The first solution is correct. The fifth item is index 4. Kuphryn

      M 1 Reply Last reply
      0
      • V valikac

        The first solution is correct. The fifth item is index 4. Kuphryn

        M Offline
        M Offline
        Mathias S
        wrote on last edited by
        #3

        My bad. pData[5]->x = 3; // err is not error.. it works.. i got a complie error. but now when i watch more carefully I saw that the error was something else.. aahh stupid me.. been working to much today. /Mathias

        1 Reply Last reply
        0
        • M Mathias S

          struct Data { int x; int y; }; Data* pData = new Data[30]; how do I access value x in item 5 in the Data array pData[5]->x = 3; // err pData[5].x = 3; //err /Mathias

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #4

          pData[5].x = 3; Since this is an array of structures, not pointers, this is the correct way to access it. (The brackets dereference the pData pointer.) Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

          R 1 Reply Last reply
          0
          • J Joe Woodbury

            pData[5].x = 3; Since this is an array of structures, not pointers, this is the correct way to access it. (The brackets dereference the pData pointer.) Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

            R Offline
            R Offline
            Ravi Bhavnani
            wrote on last edited by
            #5

            Joe Woodbury wrote: The brackets dereference the pData pointer. Actually they don't. The dereferencing has to happen explicitly, as in:    *((Data *) pData[5]).x = 3;

            PLEASE IGNORE THIS INCORRECT POST.  I PROMISE NOT TO REPLY TO ANY CP POSTS UNTIL I'M REASONABLY AWAKE.

            /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com

            T 1 Reply Last reply
            0
            • R Ravi Bhavnani

              Joe Woodbury wrote: The brackets dereference the pData pointer. Actually they don't. The dereferencing has to happen explicitly, as in:    *((Data *) pData[5]).x = 3;

              PLEASE IGNORE THIS INCORRECT POST.  I PROMISE NOT TO REPLY TO ANY CP POSTS UNTIL I'M REASONABLY AWAKE.

              /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com

              T Offline
              T Offline
              Tim Smith
              wrote on last edited by
              #6

              No, the second compiles just fine. He allocating an array of structures, not an array of pointers to the structure. Tim Smith I'm going to patent thought. I have yet to see any prior art.

              R 1 Reply Last reply
              0
              • T Tim Smith

                No, the second compiles just fine. He allocating an array of structures, not an array of pointers to the structure. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                R Offline
                R Offline
                Ravi Bhavnani
                wrote on last edited by
                #7

                Gak! You're right of course! :-O /ravi Let's put "civil" back in "civilization" Home | Articles | Freeware | Music ravib@ravib.com

                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