Reference a struct member variable
-
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
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]; -
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
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.
-
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
You have something over 20,000
float
s in each of your structures; are you really sure this is what you want?Programming is work, it isn't finger painting. Luc Pattyn
-
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
- 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
-
You have something over 20,000
float
s in each of your structures; are you really sure this is what you want?Programming is work, it isn't finger painting. Luc Pattyn
Yes I need that.
-
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
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
-
You have something over 20,000
float
s in each of your structures; are you really sure this is what you want?Programming is work, it isn't finger painting. Luc Pattyn
That's a great question...
-
You have something over 20,000
float
s in each of your structures; are you really sure this is what you want?Programming is work, it isn't finger painting. Luc Pattyn
-
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
-
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.
-
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
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;
}