Hey guys I'm playing with structs and trying to get one to include the other:

#include <iostream>
#include <string>

using namespace std;

typedef struct data
{
	string dat;
} DATA;

typedef struct name
{
	string na;
	string age;
	NAME * DATA;


}NAME;

int main()
{

return 0;
}

is this syntax right? it compiles but when i try to access dat through the name struc it wont :S

Cheers,

Recommended Answers

All 3 Replies

#include <iostream>
#include <string>

using namespace std;

typedef struct data
{
	string dat;
} DATA;

typedef struct name
{
	string na;
	string age;
//	NAME * DATA;
        DATA* nm;
}NAME;

If your intent is to have an object of type DATA inside the structure named NAME, then do as shown in red above. Avoid making object names the same as structure of class names -- a lot less confusion.

#include <iostream>
#include <string>

using namespace std;

typedef struct data
{
	string dat;
} DATA;

typedef struct name
{
	string na;
	string age;
//	NAME * DATA;
        DATA* nm;
}NAME;

If your intent is to have an object of type DATA inside the structure named NAME, then do as shown in red above. Avoid making object names the same as structure of class names -- a lot less confusion.

so how do I print out DAT? if i do cout << Name->data is all i can do not get access to the dat data?

cout << Name->nm.dat

assuming nm has been previously allocated with new operator

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.