Jump to content
  • Advertisement
Sign in to follow this  
MSalley

Structure pointers

This topic is 4846 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So, I have this:

struct address{
       char address[60];
       char town[25];
       char state[4];
       char zip[5];

struct contact_info{
       char email[30];
       char phone[15];
       char cell[15];}
       
struct profiles{
       char name[30];
       address profile_address;
       contact_info profile_contact_info;
       } profile_one;


Down in
int main()
I have made a pointer.
profile * one=&profile_one;
Now, to assign a value to
char email[30];
, I have to type
cin.getline(one->profile_contact_info.email, 30);
or something similar to that. The problem is, of I want to get values for
char email[30];
       char phone[15];
       char cell[15];
I'll have to type
cin.getline(one->profile_contact_info.email, 30);
three times. That takes a long time, and It would be easier without a lot of copying and pasting. Therefore, I would like to make a pointer, so all that I am required to type in is
cin.getline(one->name of a pointer I make->email, 30);
Where and how would I make a pointer like that?

Share this post


Link to post
Share on other sites
Advertisement
profile * one=&profile_one;
contact_info * curraddress = &(one->profile_contact_info);

Then,

char firstletter = curraddress->email[0];

Share this post


Link to post
Share on other sites
Quote:
Original post by MSalley
I'll have to type
cin.getline(one->profile_contact_info.email, 30);
three times.


Well, you have to type
cin.getline(one->profile_contact_info.email, 30);
cin.getline(one->profile_contact_info.phone, 15);
cin.getline(one->profile_contact_info.cell, 15);


Quote:
That takes a long time, and It would be easier without a lot of copying and pasting.


Well, you're the one who picked the names. And no, it's not that much typing. [wink]

Quote:
Therefore, I would like to make a pointer, so all that I am required to type in is
cin.getline(one->name of a pointer I make->email, 30);
Where and how would I make a pointer like that?


You have two possible approaches, one is to use a pointer-to-member (you probably don't want to use that kind of things yet), the other is to just take the address of the existing profile_contact_info variable:

contact_info* ptr = &(one->profile_contact_info);
cin.getline(ptr->email, 30);

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!