pointer in structure

The program stops working.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 #include<iostream>
using namespace std;
struct date
{
    int d=12;
    int m=13;
    int y=14;
};
date s;
date *dob;
int main()
{
    cout<<"Output using structure variable"<<endl;
    cout<<s.d<<s.m<<s.y<<endl;
    cout<<"Output using structure pointer"<<endl;
    cout<<dob->d<<dob->m<<dob->y<<endl;
    return 0;
}
dob doesn't point to anywhere.
what?
You need to use the new operator. Since you didn't allocate the pointer to the heap.
Last edited on
dob is a pointer. It needs to actually point to an object to be of any use.

One way to do this is to point it at a new, dynamically allocated object, as Hengry suggests.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace std;
struct date
{
    int d=12;
    int m=13;
    int y=14;
};
date s;
date *dob;
dob=&date;
int main()
{
    cout<<"Output using structure variable"<<endl;
    cout<<s.d<<s.m<<s.y<<endl;
    cout<<"Output using structure pointer"<<endl;
    cout<<dob->d<<dob->m<<dob->y<<endl;
    return 0;
}


doesnt work:(
Last edited on
I'll ask the same question as I asked in your other thread - do you actually understand what a pointer is?

If not, your time would be better spent reading your textbook to get an understanding of them, rather than randomly writing code you don't understand, to see if it works.

dob=&date;
makes no sense. date is a type, not an object.
Last edited on
yes pointer points to the address.
No. A pointer is an address. A pointer points to an object (if you're using it right).

In that line of code I quoted, what are you trying to get the address of?
address of values stored in date
it work now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;
struct date
{
    int d=12;
    int m=13;
    int y=14;
};
date s;
date *dob=&s;
int main()
{
    cout<<"Output using structure variable"<<endl;
    cout<<s.d<<s.m<<s.y<<endl;
    cout<<"Output using structure pointer"<<endl;
    cout<<dob->d<<dob->m<<dob->y<<endl;
    return 0;
}
Which date? Every date object will have a different address. To repeat myself:
MikeyBoy wrote:
date is a type, not an object.
it work now

Looks like we cross-posted. Yes, it's working because your pointer is now pointing to an actual object.
This also works( see line 10)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;
struct date
{
    int d=12;
    int m=13;
    int y=14;
};
date s;
date *dob=new date;
int main()
{
    cout<<"Output using structure variable"<<endl;
    cout<<s.d<<s.m<<s.y<<endl;
    cout<<"Output using structure pointer"<<endl;
    cout<<dob->d<<dob->m<<dob->y<<endl;
    return 0;
}
Last edited on
Yes. I assume you understand the difference between the two programs?
new gives the pointer memory from free space.
and &s gives the pointer its address.
Yes, but a very important difference is that in the first version, s and *dob are the same object, whereas in the second version, they are different objects.
Registered users can post here. Sign in or register to post.