How can I assign an address to a pointer that is a member of a structure?

I understand the concept of pointers declared as structure "variables". But I'm a little stuck when it comes to understanding members that are pointers in a structure. Structure pointers can only be assigned the address of another structure variable. But how the heck can I use a pointer member? I know i can't use the pointer structure operator (->) and I have to include the (*) operator. But i cant seem to assign an address to a member pointer.

Can anyone enlighten me on how pointer members work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
include <iostream>

using namespace std;

struct pointers
{

int number;

int number2;

int *pointer1;					

}variable1,variable2,variable3, *point;		

int main ()
{

point = &variable1;	//points to structure variable

*variable2.pointer1     //I assume this statement stores the value of variable1 to the pointer? what can I do with this statement? can i even assign an address to this pointer?

}
*variable2.pointer1 this is not a statement and does not store any thing in anything.
pointer1 is a pointer to an int. while vairable2 is a variable of type struct pointers.

* is pointer dereference operator in this case. BUT you can dereference only a pointer.
To access, pointer1 via variable2, simply do variable2.pointer1. You can now assign this the address of a certain variable a with variable2.pointer1 = &a.

to access the value of the variable pointed to by pointer1 of variable2,
do vairalbe2.*(pointer1).
You need to dereference it this way *variable2.pointer1
OK just to get this straight. In order to use a member that is a pointer in a structure, I must include a structure variable with the pointer separated by the dot operator(.) right?

Is that the only way to access pointer members?

for example
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
35
#include <iostream>

using namespace std;

struct pointers
{
int number;

int number2;

int *pointer1;

}variable1,variable2,variable3, *point;

int main ()
{

    point = &variable1;             //can the member pointer1 be used the same way as a structure pointer in terms of assigning an address?
 
    point->number = 15;

    cout << point->number << "\n";




    variable2.number2 = 10;        

    variable3.pointer1 = &variable2.number2;          //is this the only way to assign an address to a member pointer?

    cout << *variable3.pointer1;


}
can the member pointer1 be used the same way as a structure pointer in terms of assigning an address?
YES
As you will do point = &variable1, you can do
variable3.pointer1 = &variable2.number2; which is exactly what you've done.

is this the only way to assign an address to a member pointer?
YES
The only way to assign as address to a pointer is to say, pointer = &variable which is exactly what you've done.
Thank you shadowcode, but what I meant by "the only way" was:

Is it possible to simply assign an address to a member pointer this way?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace;

struct pointers
{
int number;

int number2;

int *pointer1;

}variable1,variable2,variable3, *point;


int main ()
{
     pointer1 = &variable1.number2;        //is this valid in any way?




}
Last edited on
pointer1 is a member of a structure which has many instances. variables1 to 3.
Hence, there is a copy of pointer1 in each of these variables.
That is why, you assign an address to pointer1, you need to provide which poiter1 you are talking about.
1
2
3
int a,b;
variable1.pointer1 = &a;
variable2.pointer1 = &b; //the other copy of pointer1 
Ohh I finally understand now, thank you.
Had same problem. Thanks a lot. Now try to fix.
Topic archived. No new replies allowed.