How Do I: Nested struct

Hi guys, I have a little bit problem with my code. I had declare a pointer which is member to a nested structure. I pointed it to a string where the string is also located in the nested structure. But when I want to initialize the pointer with a constant string in main(), suddenly when I run it I get 'processor fault' but no warning or even error occur when I compile it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//microsoft visual C++ 2010
#include<iostream>
#include<string>
using namespace std;

struct tagIF
{
	string binary;

	struct tagEA	//nested struct
	{
		string reg,mode;
	}EA,*pEA;
};

struct tagCLR : tagIF
{
	string DEF,B,W,L;

	tagCLR():DEF("01000010"), B("00"), W("01"), L("10") {}
}CLR;

void main()
{
	CLR.EA.mode="010";
	CLR.EA.reg="101";
	CLR.binary = CLR.DEF + CLR.B + CLR.EA.mode + CLR.EA.reg;
	cout<<hex<<strtol(&CLR.binary[0],0,2)<<"h\n\n";

	CLR.pEA->mode="010";		//compile: OK	 ,		run: processor fault
	CLR.pEA->reg="101";		//compile: OK	 ,		run: processor fault
	CLR.binary = CLR.DEF + CLR.B + CLR.pEA->mode + CLR.pEA->reg;
	cout<<hex<<strtol(&CLR.binary[0],0,2)<<"h\n\n";
}

Actually I want to use the 'this pointer'( -> ) to point at the string because I want my code looks logical and neater. I have tried to fix it with many times, search it on the internet, asking my friends, my lecturers. until present, I still fix it, but to no avail. Please show me how to fix it and tell me why.
I'm demanding your generosity.
Last edited on
Well, for one thing, I don't think you can nest structs that way. You can do this:
1
2
3
4
5
6
7
struct aStruct {
  // Members
}

struct bStruct {
   aStruct ab;
}

I'm actually confused that it compiled. Then again, I might be completely wrong.
thanks, you mean like this right?

1
2
3
4
5
6
7
8
9
struct tagEA
{
        string reg,mode;
}/*EA, *pEA*/;
struct tagIF
{
	tagEA EA, *pEA;
	string binary;
};


if so, I still get the same error, sorry. I'm actually want to write the code that can fetch and assign the data in struct like this:

expression1.expression2->expression3;

so, any suggestion? do I need to change my code writing?
Last edited on
I think line 30 in your first post needs CLR.pEA between brackets. After all, that's the entire name for the pointer. Try (CLR.pEA)->mode ....

But, to be perfectly honest, I find nesting pointer structs very confusing.
What is the error you are getting?
thanks Gaminic,
=D
This is the first time you encounter nesting structure, right?
=D

it's OK right now I have found the way how to fix it!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//this is Gaminic style
struct tagEA
{
	string reg,mode;
}EA,*pEA;

struct tagIF
{
	tagEA EA,*pEA;

	string binary;

	tagIF():pEA(&EA){}
};


or like this...

1
2
3
4
5
6
7
8
9
10
11
12
//this is my style
struct tagIF
{
	string binary;

	struct tagEA	//nested struct
	{
		string reg,mode;
	}EA,*pEA;

	tagIF():pEA(new tagEA) {}
};


Thanks friend, you give me a good inspiration... :)
It's fine Nisheeth, problem has solved, thanks. :)
Topic archived. No new replies allowed.