943,741 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 593
  • C++ RSS
Mar 26th, 2009
0

array of structs

Expand Post »
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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Nemoticchigga is offline Offline
98 posts
since Feb 2008
Mar 26th, 2009
0

Re: array of structs

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.
Reputation Points: 76
Solved Threads: 40
Junior Poster
MrSpigot is offline Offline
156 posts
since Mar 2009
Mar 26th, 2009
0

Re: array of structs

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.
Reputation Points: 395
Solved Threads: 71
Posting Whiz
jencas is offline Offline
362 posts
since Dec 2007
Mar 26th, 2009
0

Re: array of structs

you can also do it like Jenca's second suggestion like:

c++ Syntax (Toggle Plain Text)
  1. foo *test;
  2. foo = new struct foo[10];
Last edited by SeeTheLite; Mar 26th, 2009 at 4:35 pm.
Reputation Points: 38
Solved Threads: 13
Junior Poster
SeeTheLite is offline Offline
109 posts
since Mar 2009
Mar 26th, 2009
0

Re: array of structs

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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Nemoticchigga is offline Offline
98 posts
since Feb 2008
Mar 26th, 2009
0

Re: array of structs

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.
Reputation Points: 76
Solved Threads: 40
Junior Poster
MrSpigot is offline Offline
156 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Iterators act like numbers?
Next Thread in C++ Forum Timeline: vector fanction





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC