working with array of structs

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2006
Posts: 14
Reputation: DynamitMsk is an unknown quantity at this point 
Solved Threads: 0
DynamitMsk DynamitMsk is offline Offline
Newbie Poster

working with array of structs

 
0
  #1
Dec 22nd, 2006
For my project I'm using an array of structs that I declared in the header file

  1. struct City{
  2. string name;
  3. int visits;
  4. };
  5.  
  6. City* map[200];

now when I'm trying to initialize it in a constructor, like

  1. for (int ii=0; ii<200; ii++)
  2. map[ii].visits = 0;

I get an error:

left of '.visits' must have class/struct/union

Is that due to the map being a pointer? Anyone can point me in a right direction for solving this problem?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: working with array of structs

 
0
  #2
Dec 22nd, 2006
Are we using c or c++?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: working with array of structs

 
0
  #3
Dec 22nd, 2006
When you are doing City* map[200] , map is an array of 200 pointers to the type City. So in order to access the members of the City struct using its pointers you need to use the -> operator.

Here are the two ways you can do it:
  1. *map[i].visits = 100 ;
  2. // OR
  3. map[i]->visits = 100 ;

But, even if you do so your program won't work. Why ? Because you are not allocating memory to the pointers so acutally what they are poniting to is junk i.e. memory which doesn't belong to you. Consider allocating memory to the pointer before using it in your program.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,124
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: working with array of structs

 
0
  #4
Dec 22nd, 2006
Do, to piggy back on the answer by Mr. ~s.o.s~ :mrgreen: you should probably just declare the map as City map[200];. That would fix your problem.

I also hope this line is not in your header file. It should be in your code file only.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: working with array of structs

 
0
  #5
Dec 22nd, 2006
Originally Posted by WaltP View Post
Do, to piggy back on the answer by Mr. ~s.o.s~ :mrgreen: you should probably just declare the map as City map[200];. That would fix your problem.

I also hope this line is not in your header file. It should be in your code file only.
Bleh..I wrongly assumed that the project required him to dynamically allocate memory, otherwise would have told him the simple fix
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 14
Reputation: DynamitMsk is an unknown quantity at this point 
Solved Threads: 0
DynamitMsk DynamitMsk is offline Offline
Newbie Poster

Re: working with array of structs

 
0
  #6
Dec 22nd, 2006
~s.o.s~

The project will require me to dynamically allocate memory, not for this array, but for a link list I'll also need to have.

Here's what I've done for this array:

in the header file I created a pointer to the struct
  1. City* map;

and in the constructor I did the following:
  1. map = new City[200];
  2. for (int ii=0; ii<200; ii++)
  3. map[ii].visits = 0;

How is this solution? I believe this is how I'd be alocating memory dynamically, if I needed to. Just have City[array_size_variable]. Is this correct? Thank you for your help.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: working with array of structs

 
0
  #7
Dec 22nd, 2006
One important thing in programming is to learn to evaluate your own code by writing it down and testing it with the help of the compiler.

Does the code which you pasted give you any errors ? No, then you are right atleast for the time being. Keep experimenting and you would learn two fold than what you are learning now.

And btw, yes your code is fine for the time being.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,124
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: working with array of structs

 
0
  #8
Dec 22nd, 2006
Originally Posted by DynamitMsk View Post
~s.o.s~

The project will require me to dynamically allocate memory, not for this array, but for a link list I'll also need to have.

Here's what I've done for this array:

in the header file I created a pointer to the struct
  1. City* map;

and in the constructor I did the following:
  1. map = new City[200];
  2. for (int ii=0; ii<200; ii++)
  3. map[ii].visits = 0;

How is this solution? I believe this is how I'd be alocating memory dynamically, if I needed to. Just have City[array_size_variable]. Is this correct? Thank you for your help.
No. You probably want:
  1. City* map[200]; // declare an array of pointers
  2.  
  3. ...
  4.  
  5. for (int ii=0; ii<200; ii++)
  6. {
  7. map[ii] = new City; // load each pointer with a structure
  8. }


Originally Posted by s.o.s.
And btw, yes your code is fine for the time being.
It is? If so, I stand corrected. I've never seen it done that way.
Last edited by WaltP; Dec 22nd, 2006 at 4:53 pm.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,649
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: working with array of structs

 
0
  #9
Dec 22nd, 2006
Originally Posted by WaltP View Post
No. You probably want:
  1. City* map[200]; // declare an array of pointers
  2.  
  3. ...
  4.  
  5. for (int ii=0; ii<200; ii++)
  6. {
  7. map[ii] = new City; // load each pointer with a structure
  8. }

It is? If so, I stand corrected. I've never seen it done that way.
If it is about the semantics, then yes I agree, what I presented is not array of pointers, but if going implementation wise, both the solutions are the same. But mine with less overhead since the constructor is not getting called in the loop.

Just an example of pointer being used to emulate an array...

An eg.
  1. #include <iostream>
  2. #include <string>
  3. using namespace std ;
  4.  
  5. struct City
  6. {
  7. string name ;
  8. int visit ;
  9. } ;
  10.  
  11. int main( )
  12. {
  13. cout << endl << "~s.o.s~'s method" << endl ;
  14. City* map = new City[3] ;
  15. for( int i = 0; i < 3; ++i )
  16. {
  17. cout << "Enter name: " ;
  18. cin >> map[i].name ;
  19. getchar( ) ;
  20. }
  21.  
  22. for( int i = 0; i < 3; ++i )
  23. {
  24. cout << map[i].name << endl ;
  25. }
  26.  
  27. cout << endl << "Mr. WaltP's method" << endl ;
  28.  
  29. City* mapWalt[3] ;
  30.  
  31. for( int i = 0; i < 3; ++i )
  32. {
  33. mapWalt[i] = new City ;
  34. cout << "Enter name: " ;
  35. cin >> mapWalt[i]->name ;
  36. getchar( ) ;
  37. }
  38.  
  39. for( int i = 0; i < 3; ++i )
  40. {
  41. cout << mapWalt[i]->name << endl ;
  42. }
  43.  
  44. }

My output:
~s.o.s~'s method
Enter name: abc
Enter name: efg
Enter name: hij

abc
efg
hij

Mr. WaltP's method
Enter name: abc
Enter name: efg
Enter name: hij
abc
efg
hij
In the case of array of pointers, to delete the enteries, you will have to loop through the array once while in my case it would be a simple matter of using the delete[] syntax.
Last edited by ~s.o.s~; Dec 22nd, 2006 at 5:17 pm.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: working with array of structs

 
0
  #10
Dec 22nd, 2006
I can't believe you're using new without delete[].

How are you a moderator again? Ha ha.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3667 | Replies: 14
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC