Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

[C++] Accessing Pointer Data within a struct

I'm seeking a way to hold pointers inside a struct, and need some pointers (pun absolutely intended) as to how to do it.

I'm trying to access the data contained within the pointer (and not just the address), but I'm getting compile-time errors :(.

This is what I've tried:
#include <iostream>
using namespace std;

typedef struct test{
int *myint;
}TEST;

int main()
{
test mytest;
mytest.myint = new int;
cin >> mytest.*myint;
cout << mytest.*myint << endl;
return 0;
}

However the 2 lines above "return 0" get seen as functions. How would I go about accessing the data within a pointer that is within a struct, or is it even possible?

Thanks in advance

-- El Sid
[813 byte] By [el__sid] at [2007-11-11 10:09:23]
# 1 Re: [C++] Accessing Pointer Data within a struct
#include <iostream>
using namespace std;

typedef struct test{
int *myint;
}TEST;

int main()
{
test mytest;
mytest.myint = new int;
cin >>*( mytest.myint);
cout <<*( mytest.myint ) << endl;
return 0;
}

This will definitely work.. Try it.
vikikishor at 2007-11-11 20:59:16 >
# 2 Re: [C++] Accessing Pointer Data within a struct
Fantastic, thanks a lot!

Just out of curiosity, if I were to instantiate the struct as a pointer within main, AND have the pointer within the struct declaration, would the syntax be "**mytest.myint" or would having a pointer within a pointer break things?
el__sid at 2007-11-11 21:00:22 >
# 3 Re: [C++] Accessing Pointer Data within a struct
You can make pointers as deep as you like, but it may be un-necessary. You can also assign pointers to each other, so you can keep the single pointer inside the struct and simply assign it from the pointer you have inside main without the second level of complexity.
jonnin at 2007-11-11 21:01:27 >
# 4 Re: [C++] Accessing Pointer Data within a struct
You probably meant this:

struct test{
int *myint;
};

int main()
{
test *ptest = new test;
ptest->myint = new int;
cin >>*( ptest->myint);
cout <<*(ptest->myint ) << endl;
return 0;
}
Danny at 2007-11-11 21:02:21 >
# 5 Re: [C++] Accessing Pointer Data within a struct
You can make pointers as deep as you like, but it may be un-necessary. You can also assign pointers to each other, so you can keep the single pointer inside the struct and simply assign it from the pointer you have inside main without the second level of complexity.

I realise that it's a farse, unfortunately however it's one of the requirements for the project I'm doing to use a struct, and my project required that my struct was a pointer (for use as a linked list) and contained a pointer (for my own personal requirements).

Thanks for your concern though :)

Thanks Danny, that was exactly what I needed!

And thanks all for your replies.
el__sid at 2007-11-11 21:03:20 >
# 6 Re: [C++] Accessing Pointer Data within a struct
a linked list looks sort of like

struct alist
{
type data;
alist * next;
};

with whatever else you want inside it for extra data or anything. You set next to null on the last item in the list and test against that when you iterate through it. Adding items at the top is most efficient -- your new item.next = top, top = new item, bam your hooked up. Unless the order they are in matters. Thus linked lists are passable when used as a stack or queue (you can track the last item for a queue) and fairly sluggish for most other tasks (you spend a lot of time chasing pointers to find what you wanted).
jonnin at 2007-11-11 21:04:31 >
# 7 Re: [C++] Accessing Pointer Data within a struct
I already had the list working, just needed the extra pointer which referred to something else. Thanks though
el__sid at 2007-11-11 21:05:31 >