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. Reference a struct member variable

Reference a struct member variable

Scheduled Pinned Locked Moved C / C++ / MFC
databasetutorial
12 Posts 8 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
    manoharbalu
    wrote on last edited by
    #1

    I have 2 different structures test1 and test2 struct test1 { float IV [300]; char FAIL [200]; char FD [600]; float NENTH [600]; float NKVAL [30][600]; float NLEVEL [600]; int COUNT; int CTIME; }; struct test2 { float RIV [300]; float RXX [100]; char RFAIL [200]; char RFD [600]; float RTB [50]; float RNENTH [600]; char GCD [200]; float RNKVAL [30][600]; char MTBD [600]; float RNLEVEL [600]; int RCOUNT; int RCTIME; }; The 2 structure objects are: struct test1 *sim1; struct test2 *sim2; sim1 = new test1(); sim2 = new test2(); Now I want the sim1->IV to exactly point to sim2->RIV and sim1->NENTH to exactly point to sim2->RNENTH. So at any point of time, sim1->IV[i] should be same as sim2->RIV[i] where i is any of the valid index range and similarly for NENTH and RNENTH. Any one know how to acheive this please provide me the code.

    C.B.Mohankumar

    C CPalliniC L A T 7 Replies Last reply
    0
    • M manoharbalu

      I have 2 different structures test1 and test2 struct test1 { float IV [300]; char FAIL [200]; char FD [600]; float NENTH [600]; float NKVAL [30][600]; float NLEVEL [600]; int COUNT; int CTIME; }; struct test2 { float RIV [300]; float RXX [100]; char RFAIL [200]; char RFD [600]; float RTB [50]; float RNENTH [600]; char GCD [200]; float RNKVAL [30][600]; char MTBD [600]; float RNLEVEL [600]; int RCOUNT; int RCTIME; }; The 2 structure objects are: struct test1 *sim1; struct test2 *sim2; sim1 = new test1(); sim2 = new test2(); Now I want the sim1->IV to exactly point to sim2->RIV and sim1->NENTH to exactly point to sim2->RNENTH. So at any point of time, sim1->IV[i] should be same as sim2->RIV[i] where i is any of the valid index range and similarly for NENTH and RNENTH. Any one know how to acheive this please provide me the code.

      C.B.Mohankumar

      C Offline
      C Offline
      Chris Losinger
      wrote on last edited by
      #2

      manoharbalu wrote:

      Now I want the sim1->IV to exactly point to sim2->RIV

      so you need to do this:

      struct test1
      {
      float *IV;
      ...

      and when you create a test1 object, you have to set the pointer to a test2's RIV array:

      sim1 = new test1();
      sim2 = new test2();
      sim1->IV = &sim2->RIV[0];

      image processing toolkits | batch image processing

      1 Reply Last reply
      0
      • M manoharbalu

        I have 2 different structures test1 and test2 struct test1 { float IV [300]; char FAIL [200]; char FD [600]; float NENTH [600]; float NKVAL [30][600]; float NLEVEL [600]; int COUNT; int CTIME; }; struct test2 { float RIV [300]; float RXX [100]; char RFAIL [200]; char RFD [600]; float RTB [50]; float RNENTH [600]; char GCD [200]; float RNKVAL [30][600]; char MTBD [600]; float RNLEVEL [600]; int RCOUNT; int RCTIME; }; The 2 structure objects are: struct test1 *sim1; struct test2 *sim2; sim1 = new test1(); sim2 = new test2(); Now I want the sim1->IV to exactly point to sim2->RIV and sim1->NENTH to exactly point to sim2->RNENTH. So at any point of time, sim1->IV[i] should be same as sim2->RIV[i] where i is any of the valid index range and similarly for NENTH and RNENTH. Any one know how to acheive this please provide me the code.

        C.B.Mohankumar

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        manoharbalu wrote:

        Now I want the sim1->IV to exactly point to sim2->RIV
        and sim1->NENTH to exactly point to sim2->RNENTH.

        If you need a pointer then use a pointer:

        struct test1
        {
        float * IV;
        //...
        float * NENTH;
        //...
        };
        //...

        test1 *sim1;
        test2 *sim2;

        sim2 = new test2();
        sim1 = new test1();
        sim1->IV = sim2->RIV;
        sim1->NENTH = sim2->RNENTH;

        Veni, vidi, vici.

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • M manoharbalu

          I have 2 different structures test1 and test2 struct test1 { float IV [300]; char FAIL [200]; char FD [600]; float NENTH [600]; float NKVAL [30][600]; float NLEVEL [600]; int COUNT; int CTIME; }; struct test2 { float RIV [300]; float RXX [100]; char RFAIL [200]; char RFD [600]; float RTB [50]; float RNENTH [600]; char GCD [200]; float RNKVAL [30][600]; char MTBD [600]; float RNLEVEL [600]; int RCOUNT; int RCTIME; }; The 2 structure objects are: struct test1 *sim1; struct test2 *sim2; sim1 = new test1(); sim2 = new test2(); Now I want the sim1->IV to exactly point to sim2->RIV and sim1->NENTH to exactly point to sim2->RNENTH. So at any point of time, sim1->IV[i] should be same as sim2->RIV[i] where i is any of the valid index range and similarly for NENTH and RNENTH. Any one know how to acheive this please provide me the code.

          C.B.Mohankumar

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You have something over 20,000 floats in each of your structures; are you really sure this is what you want?

          Programming is work, it isn't finger painting. Luc Pattyn

          M A L 3 Replies Last reply
          0
          • M manoharbalu

            I have 2 different structures test1 and test2 struct test1 { float IV [300]; char FAIL [200]; char FD [600]; float NENTH [600]; float NKVAL [30][600]; float NLEVEL [600]; int COUNT; int CTIME; }; struct test2 { float RIV [300]; float RXX [100]; char RFAIL [200]; char RFD [600]; float RTB [50]; float RNENTH [600]; char GCD [200]; float RNKVAL [30][600]; char MTBD [600]; float RNLEVEL [600]; int RCOUNT; int RCTIME; }; The 2 structure objects are: struct test1 *sim1; struct test2 *sim2; sim1 = new test1(); sim2 = new test2(); Now I want the sim1->IV to exactly point to sim2->RIV and sim1->NENTH to exactly point to sim2->RNENTH. So at any point of time, sim1->IV[i] should be same as sim2->RIV[i] where i is any of the valid index range and similarly for NENTH and RNENTH. Any one know how to acheive this please provide me the code.

            C.B.Mohankumar

            A Offline
            A Offline
            Aescleal
            wrote on last edited by
            #5

            - Replace test1::IV with a pointer to float - Replace test1::NENTH with a pointer to float - Add a constructor for test1 that takes a test2 by reference and set up the pointers in there As Richard said are you sure you want that size of object kicking around? And are you sure you want one object to interfere directly with the internals of another object? That usually leads to tears and breaking an object's invariant. Cheers, Ash Edit as I forgot the important bit in italics

            1 Reply Last reply
            0
            • L Lost User

              You have something over 20,000 floats in each of your structures; are you really sure this is what you want?

              Programming is work, it isn't finger painting. Luc Pattyn

              M Offline
              M Offline
              manoharbalu
              wrote on last edited by
              #6

              Yes I need that.

              1 Reply Last reply
              0
              • M manoharbalu

                I have 2 different structures test1 and test2 struct test1 { float IV [300]; char FAIL [200]; char FD [600]; float NENTH [600]; float NKVAL [30][600]; float NLEVEL [600]; int COUNT; int CTIME; }; struct test2 { float RIV [300]; float RXX [100]; char RFAIL [200]; char RFD [600]; float RTB [50]; float RNENTH [600]; char GCD [200]; float RNKVAL [30][600]; char MTBD [600]; float RNLEVEL [600]; int RCOUNT; int RCTIME; }; The 2 structure objects are: struct test1 *sim1; struct test2 *sim2; sim1 = new test1(); sim2 = new test2(); Now I want the sim1->IV to exactly point to sim2->RIV and sim1->NENTH to exactly point to sim2->RNENTH. So at any point of time, sim1->IV[i] should be same as sim2->RIV[i] where i is any of the valid index range and similarly for NENTH and RNENTH. Any one know how to acheive this please provide me the code.

                C.B.Mohankumar

                T Offline
                T Offline
                TomasRiker2
                wrote on last edited by
                #7

                Here you see that arrays and pointers are not quite the same although they behave almost similarly. You can't change the address of an array once it was created.

                Visit my project: Derivative Calculator

                1 Reply Last reply
                0
                • L Lost User

                  You have something over 20,000 floats in each of your structures; are you really sure this is what you want?

                  Programming is work, it isn't finger painting. Luc Pattyn

                  A Offline
                  A Offline
                  Albert Holguin
                  wrote on last edited by
                  #8

                  That's a great question...

                  1 Reply Last reply
                  0
                  • L Lost User

                    You have something over 20,000 floats in each of your structures; are you really sure this is what you want?

                    Programming is work, it isn't finger painting. Luc Pattyn

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    yeah, and he will probably declare them on the stack.....

                    ============================== Nothing to say.

                    1 Reply Last reply
                    0
                    • M manoharbalu

                      I have 2 different structures test1 and test2 struct test1 { float IV [300]; char FAIL [200]; char FD [600]; float NENTH [600]; float NKVAL [30][600]; float NLEVEL [600]; int COUNT; int CTIME; }; struct test2 { float RIV [300]; float RXX [100]; char RFAIL [200]; char RFD [600]; float RTB [50]; float RNENTH [600]; char GCD [200]; float RNKVAL [30][600]; char MTBD [600]; float RNLEVEL [600]; int RCOUNT; int RCTIME; }; The 2 structure objects are: struct test1 *sim1; struct test2 *sim2; sim1 = new test1(); sim2 = new test2(); Now I want the sim1->IV to exactly point to sim2->RIV and sim1->NENTH to exactly point to sim2->RNENTH. So at any point of time, sim1->IV[i] should be same as sim2->RIV[i] where i is any of the valid index range and similarly for NENTH and RNENTH. Any one know how to acheive this please provide me the code.

                      C.B.Mohankumar

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      As others suggest, you need pointers, but if you really have common data shared between two structs then take it out of them and put it in a third struct.

                      ============================== Nothing to say.

                      A 1 Reply Last reply
                      0
                      • L Lost User

                        As others suggest, you need pointers, but if you really have common data shared between two structs then take it out of them and put it in a third struct.

                        ============================== Nothing to say.

                        A Offline
                        A Offline
                        Aescleal
                        wrote on last edited by
                        #11

                        A far better way of doing things than I suggested. Cheers, Ash

                        1 Reply Last reply
                        0
                        • M manoharbalu

                          I have 2 different structures test1 and test2 struct test1 { float IV [300]; char FAIL [200]; char FD [600]; float NENTH [600]; float NKVAL [30][600]; float NLEVEL [600]; int COUNT; int CTIME; }; struct test2 { float RIV [300]; float RXX [100]; char RFAIL [200]; char RFD [600]; float RTB [50]; float RNENTH [600]; char GCD [200]; float RNKVAL [30][600]; char MTBD [600]; float RNLEVEL [600]; int RCOUNT; int RCTIME; }; The 2 structure objects are: struct test1 *sim1; struct test2 *sim2; sim1 = new test1(); sim2 = new test2(); Now I want the sim1->IV to exactly point to sim2->RIV and sim1->NENTH to exactly point to sim2->RNENTH. So at any point of time, sim1->IV[i] should be same as sim2->RIV[i] where i is any of the valid index range and similarly for NENTH and RNENTH. Any one know how to acheive this please provide me the code.

                          C.B.Mohankumar

                          S Offline
                          S Offline
                          Stefan_Lang
                          wrote on last edited by
                          #12

                          I suggest you go with the suggestion of Erudite_Erik. I took the liberty to put it into code:

                          struct testsub
                          {
                          float IV [300];
                          float NENTH [600];
                          };

                          struct test1
                          {
                          // constructor:
                          test1(float* iv, float* nenth)
                          : IV(iv), NENTH(nenth) // this initializes the pointer variables
                          {
                          }

                          float\* IV;
                          char FAIL \[200\];
                          char FD \[600\];
                          float\* NENTH;
                          float NKVAL \[30\]\[600\];
                          float NLEVEL \[600\];
                          int COUNT;
                          int CTIME;
                          };
                          

                          struct test2
                          {
                          // constructor:
                          test2(float* iv, float* nenth)
                          : RIV(iv), RNENTH(nenth) // this initializes the pointer variables
                          {
                          }

                          float\* RIV;
                          float RXX \[100\];
                          char RFAIL \[200\];
                          char RFD \[600\];
                          float RTB \[50\];
                          float\* RNENTH;
                          char GCD \[200\];
                          float RNKVAL \[30\]\[600\];
                          char MTBD \[600\];
                          float RNLEVEL \[600\];
                          int RCOUNT;
                          int RCTIME;
                          

                          };

                          void foo()
                          {
                          // allocate objects
                          struct testsub * sub = new testsub();
                          struct test1 *sim1 = new test1(sub->IV, sub->NENTH);
                          struct test2 *sim2 = new test2(sub->IV, sub->NENTH);

                          // now do some stuff
                          // ...
                          
                          // now release memory
                          delete sim2;
                          delete sim1;
                          delete sub;
                          

                          }

                          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