Pointers Within Data Structures ??

So say you have something like below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{

 struct pop {
int *a;
int b;

}


system("PAUSE");


}


How would you use or assign a value to a? I don't get how you access those since I have never used pointers like that within a structure.

Also say you have something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

int main()
{

 struct pop {
int a;
int b;

}*can


system("PAUSE");


}


The pointer int a is gone but there's a pointer to identifier can. How do you set values to a and b with can now seems impossible? I'm just not also getting this can someone explain?
Last edited on
First
1
2
pop mypop;
mypop.a = new int;
a is a normal member, you access it as any other type of member. It being a pointer only changes how you use it.

Second
1
2
3
can->a = 7;
//or
(*can).a;
The second variant should be clear. You first apply * to get the object 'can' points to and then use . as you do with all objects. -> is just a cleaner way to write the same thing.
You assign a pointer an address, either dynamically assigned or statically assigned.

In the case of the first structure:

pop pop_a, pop_b;

pop_a = new int;
pop_a = new int[10];

or

pop_a.a = &var;

for the second structure, I usually do not declare variables right off the struct definition. Instead:

1
2
3
4
5
6
7
8
9
10
struct pop {
    int a;
    int b;
};

pop pop_a, *pop_b;

pop_b = new pop;
pop_b->a = 3;
pop_b->b = 10;


or

pop_a.a = 3;
pop_a.b = 10;

pop_b = &pop_a;

Last edited on
The both examples contain error because you did not place ; after structure or pointer to sttructure definitions.

A pojnter is like other members of structure. You can assign it value the same way as and for other members.

For example,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct pop 
{
   int *a;
   int b;
};

pop str1;

str1.a = 0;
str1.b = 0;

pop str2;

str2.a = new int( 10 );
str2.b = 10;

As for the second example, you can either allocate structure in heap and assign its address to your pointer, or assign address of local object of type structure to your pointer.

For example,

1
2
3
4
5
6
7
8
9
10
struct pop 
{
   int a;
   int b;
}*can;

can = new pop;

can->a = 10;
can->b = 10;

or

1
2
3
4
5
6
7
8
9
struct pop 
{
   int a;
   int b;
}*can;

pop str = { 10, 10 };

can = &str;
Thanks you guys are amazingly helpful :)
Some of this though is still extremely annoying and hazy. I've never quite 100% got this pointer concept and I'm really trying to wrap my mind around it it just doesn't make sense.

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
#include <iostream>
#include <new>
#include <string>
using namespace std;

int main()
{

 struct pop{
int *a;
int b;

};
pop time;
time.a = new int(1);
int[] as = {10, 20};
time.a = &as;


//output
cout << time->a << " Done" << endl;

//pause
system("PAUSE");

return 0;
}


So in my code there you are creating dynamically an array of two 0 and 1 with time.a. Now let's say we want to use the values from the array as within that dynamic memory. Why can't I get it to point at that and output it? Wouldn't you then point to the memory address of as?

What am I missing can someone try to explain this in a way that I can understand it and understand the science of how to get that to then point to as?
Your code shall not be compiled because this construction

int[] as = {10, 20};

is invalid. It will be correct to write

int as[] = {10, 20};

and then

time.a = as;

and you should remove the statement

time.a = new int(1);

because you are rewritting this value by a new one which is address of the first element of the array.
I see I got that now vlad this is becoming more clear. Now lets say you used

1
2
3
4
5
6
7
8
9
10
11
12
13

int main()
{

 struct pop{
int a;
int b;

};
pop *time;
time = new pop;
time->a = 12;


That works fine but lets say I up this and use
1
2
3
4
5
6
7
8
9
10
11
12
13

int main()
{

 struct pop{
int a;
int b;

};
pop *time;
time = new pop[1];
time->a = 12;


Now there's like an array of TWO dynamically created structure pops? Really confusing.

So how would you then access pop[1] and pop[0]. Sorry this is becoming more clear but still lol?
Last edited on
No, there is an array of one element

time = new pop[1];

It should be deleted the following way

delete []time;

If you want to have more elements you can write

1
2
3
4
5
6
7
8
9
int size = 10;

time = new pop[size];

for ( int i = 0; i < size; i++ )
{
   time[i].a = i;
   time[i].b = size - 1 - i;
}


Thanks Vlad. God this stuff is just so confusing it's crazy
so we have

1
2
3
4
5
6
7
8
9
10
11
12
13
 struct pop{
int *a;
int b;

};
pop time;
int as[] = {10, 20};
time.a = new int(1);
time.a = as;


//output
cout << *time.a << " Done" << endl;


that outputs ten but say I want to output the ENTIRE array how would you do that?
As I have understood you are asking how to output all elements of as using member a of structure object time.

Well

1
2
cout << *time.a << " Done" << endl;
cout << *( time.a + 1 ) << " Done" << endl;

or

1
2
cout << ( time.a )[0] << " Done" << endl;
cout << ( time.a )[1] << " Done" << endl;
I meant outputting the entire array but you could do that with something like

1
2
3
4
5
6
for (int i = 0; i < 2; i++)
{
//output
cout << as[i] << " Done" << endl;

}


So I guess I'm getting better at this phew!. These nasty pointers
My last question with this is say you use this

1
2
3
4
5
6
7
8
9
10
11
12
 struct pop{
int a;
int b;

};
pop *time;
time = new pop;
time->b = 20;

cout << time->b << endl;

delete time;


should you use delete like for time there to free memory since you are allocating it?
Yes. In general, what you new, you should delete.
Topic archived. No new replies allowed.