array of structs

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

Join Date: Feb 2008
Posts: 98
Reputation: Nemoticchigga is an unknown quantity at this point 
Solved Threads: 2
Nemoticchigga Nemoticchigga is offline Offline
Junior Poster in Training

array of structs

 
0
  #1
Mar 26th, 2009
I have a struct. I want to declare a pointer to that struct. Now I want 10 of those structs. How is this declared? I have tried (call the struct foo for example):

foo* test[10];

when I make a call to test[0]->whatever or test[5]->whatever it seems to always write to the first element (the 0th of the array).

I think what I'm getting is an array of pointers. I want a pointer to an array of structs.

How do I delare this? Thanks.
Last edited by Nemoticchigga; Mar 26th, 2009 at 11:17 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 145
Reputation: MrSpigot is on a distinguished road 
Solved Threads: 34
MrSpigot's Avatar
MrSpigot MrSpigot is offline Offline
Junior Poster

Re: array of structs

 
0
  #2
Mar 26th, 2009
Yes, you are getting an array of pointers.

Instead, declare your array of structs:
foo test[10];

Now you have 10 foo objects and foo[0].myMember will reference the contents of the first one.

To get a pointer you then use:
foo* pFoo = &test[0];
which will give you a pointer to the first one.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 360
Reputation: jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice jencas is just really nice 
Solved Threads: 69
jencas jencas is offline Offline
Posting Whiz

Re: array of structs

 
0
  #3
Mar 26th, 2009
Correction: you are getting an array of uninitialized pointers. Accessing them may result in undefined behaviour. So do it like MrSpigot recommends or use new to initialize the array members.
If you are forced to reinvent the wheel at least try to invent a better one!

Please use code tags - Please mark solved threads as solved
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 109
Reputation: SeeTheLite is an unknown quantity at this point 
Solved Threads: 12
SeeTheLite SeeTheLite is offline Offline
Junior Poster

Re: array of structs

 
0
  #4
Mar 26th, 2009
you can also do it like Jenca's second suggestion like:

  1. foo *test;
  2. foo = new struct foo[10];
Last edited by SeeTheLite; Mar 26th, 2009 at 4:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 98
Reputation: Nemoticchigga is an unknown quantity at this point 
Solved Threads: 2
Nemoticchigga Nemoticchigga is offline Offline
Junior Poster in Training

Re: array of structs

 
0
  #5
Mar 26th, 2009
Thanks all.

MrSpigot, how would I then access the structs through the pointer?

pFoo->test[0].index = blah; does not work. How would this be done?

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 145
Reputation: MrSpigot is on a distinguished road 
Solved Threads: 34
MrSpigot's Avatar
MrSpigot MrSpigot is offline Offline
Junior Poster

Re: array of structs

 
0
  #6
Mar 26th, 2009
Originally Posted by Nemoticchigga View Post
Thanks all.

MrSpigot, how would I then access the structs through the pointer?

pFoo->test[0].index = blah; does not work. How would this be done?

Thanks.
pFoo->index = blah;
will access the first struct, but more generally you could use pFoo[n].index = blah;

Or modify pFoo:
pFoo += 1;
pFoo->index = blah; // pFoo now references the second struct
Last edited by MrSpigot; Mar 26th, 2009 at 6:18 pm.
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: 358 | Replies: 5
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC