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?
include <iostream>
usingnamespace 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).
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?
#include <iostream>
usingnamespace 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;
}
#include <iostream>
usingnamespace;
struct pointers
{
int number;
int number2;
int *pointer1;
}variable1,variable2,variable3, *point;
int main ()
{
pointer1 = &variable1.number2; //is this valid in any way?
}
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