943,922 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2436
  • C++ RSS
Apr 10th, 2008
0

Dynamic array of structures

Expand Post »
Hi all
I'm trying to create a dynamic array of structures and have come across the following code but I can't figure out how some of it works: (incidentally this is from Prata's book C++ Primer Plus, Ch4, Ex.9)

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. //structure declaration
  7. struct CandyBar
  8. {
  9. char brand[20];
  10. float weight;
  11. int calories;
  12. };
  13.  
  14. //dynamic array declaration
  15. CandyBar *Candies = new CandyBar[3];
  16.  
  17. //create pointer to the first CandyBar structure within the array
  18. CandyBar *CandyPointer = &Candies[0];
  19.  
  20. //structure initialization one by one
  21. (*CandyPointer).brand = "Mocha Munch";
  22. CandyPointer->weight = 2.3;
  23. CandyPointer->calories = 350;
  24.  
  25. <snipped code>
  26.  
  27. return 0;
  28. }

As far as I can understand - line 15
Quote ...
CandyBar *Candies = new CandyBar[3];
creates a pointer (Candies) which contains the address of the first element in the array (which in this case is a structure of CandyBar type)

line 18
Quote ...
CandyBar *CandyPointer = &Candies[0];
creates a pointer (CandyPointer) which points to the address of the previous pointer when it's pointing at the first element.

Why do we need this second pointer to initialise the array elements, rather than using the first pointer?
e.g.
C++ Syntax (Toggle Plain Text)
  1. CandyBar *Candies = new CandyBar[3];
  2. Candies[0] = {"Violent Crumple", 20.5, 7000};
which I know doesn't work (syntax error) - but why not??

Thanks a lot
cobberas
Reputation Points: 10
Solved Threads: 0
Light Poster
cobberas is offline Offline
28 posts
since Apr 2008
Apr 10th, 2008
0

Re: Dynamic array of structures

when we do

CandyBar *Candies = new CandyBar[3];

we are allocating memory to hold 3 elements of CandyBar type and returning the pointer to the base address of this memory. This address will actually be same as the address of the 0th element.

both CandyPointer abd Candies point the same memory location. however for better understanding he has assigned the address of the 0th element to CandyPointer. also if he were to put this in a for loop and access each element of the array then he can use the same pointer and not lose the base address.

most importantly in C++ arrays, index starts from 0. That is the 1st element is Candies [0]
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Apr 10th, 2008
0

Re: Dynamic array of structures

The pointer is not needed. Perhaps this code is simply a means to demonstrate how you might access the data either via its array element or a pointer to an element. For example, lines 21-23 might also be written as:
C++ Syntax (Toggle Plain Text)
  1. strcpy( Candies[0].brand , "Mocha Munch" ); //see below
  2. Candies[0].weight = 2.3;
  3. Candies[0].calories = 350;
Do you want to iterate a pointer or an index variable? You've got options.

And while we're at it, line 21 as originally written doesn't work, you can't use the assignment operator to copy to the C-style string.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007
Apr 10th, 2008
0

Re: Dynamic array of structures

Click to Expand / Collapse  Quote originally posted by cobberas ...
C++ Syntax (Toggle Plain Text)
  1. Candies[0] = {"Violent Crumple", 20.5, 7000};
which I know doesn't work (syntax error) - but why not??
I think this will be possible in the next version of C++
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
Apr 10th, 2008
0

Re: Dynamic array of structures

That's great - thanks heaps.

Quote ...
both CandyPointer abd Candies point the same memory location
That's what I thought; glad I undersdtood this correctly!

Quote ...
if he were to put this in a for loop and access each element of the array then he can use the same pointer and not lose the base address.
OK. Good point; I hadn't thought of that - I'm not up to that chapter of the book yet :-)

Quote ...
Do you want to iterate a pointer or an index variable?
The latter, as you've shown in your sample code - thanks for that, it's exactly what I was trying to do (don't you just love novice programmers?!!)

I guess if I was wanting to access the array elements without losing the base address I'd iterate a pointer to the array rather than an index variable.

Quote ...
line 21 as originally written doesn't work, you can't use the assignment operator to copy to the C-style string.
Quite so - thanks!

Cheers
Cobberas
Reputation Points: 10
Solved Threads: 0
Light Poster
cobberas is offline Offline
28 posts
since Apr 2008
Apr 10th, 2008
0

Re: Dynamic array of structures

>>I think this will be possible in the next version of C++
You can do that now

C++ Syntax (Toggle Plain Text)
  1. struct CandyBar
  2. {
  3. public:
  4. CandyBar (const char* brnd, float wt, int calors)
  5. : brand(brnd), weight(wt), calories(calors)
  6. {
  7. }
  8. string brand;
  9. float weight;
  10. int calories;
  11. };
  12.  
  13. int main()
  14. {
  15. CandyBar Bars("Violent Crumple", 20.5, 7000);
  16. }

Apparently you can't make Bars and array, such as Bars[3] and use initialization lists like the above. Tried it but the compiler didn't like it.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Apr 10th, 2008
0

Re: Dynamic array of structures

For any other newbies out there having trouble with this exercise from Stephen Prata's book (and I've seen a few queries about this one on the 'net), here's the final solution I came up with:

C++ Syntax (Toggle Plain Text)
  1. //-------------------------------------------------------------------------
  2. // Programming Exercise 4.9
  3. //-------------------------------------------------------------------------
  4.  
  5. // PREPROCESSOR directive to include contents of the iostream & string files
  6. #include <iostream>
  7. #include <string>
  8.  
  9. struct CandyBar
  10. {
  11. std::string brand;
  12. float weight;
  13. int calories;
  14. };
  15.  
  16. int main(int argc, char* argv[])
  17. {
  18. // make definitions made in the std namespace visible to the program
  19. using namespace std;
  20.  
  21. //create a dynamic array of structures, & assign its address to a pointer
  22. //called 'ptrBar'
  23. //NB: ptrBar points to the 1st element of the array
  24. CandyBar * ptrBar = new CandyBar[3];
  25.  
  26. //initialise the 3 arrays using the ptrBar pointer
  27. ptrBar[0].brand = "Mocha Munch";
  28. ptrBar[0].weight = 2.3;
  29. ptrBar[0].calories = 350;
  30.  
  31. ptrBar[1].brand = "Violent Crumple";
  32. ptrBar[1].weight = 1.5;
  33. ptrBar[1].calories = 650;
  34.  
  35. ptrBar[2].brand = "Cherry Delight";
  36. ptrBar[2].weight = 4.9;
  37. ptrBar[2].calories = 380;
  38.  
  39. //display contents of each structure in the array
  40. cout << (ptrBar[0].brand).data() << " candy bar weighs " << ptrBar[0].weight
  41. << " grams and contains " << ptrBar[0].calories << " calories.\n";
  42. cout << (ptrBar[1].brand).data() << " candy bar weighs " << ptrBar[1].weight
  43. << " grams and contains " << ptrBar[1].calories << " calories.\n";
  44. cout << (ptrBar[2].brand).data() << " candy bar weighs " << ptrBar[2].weight
  45. << " grams and contains " << ptrBar[2].calories << " calories.\n";
  46.  
  47. cin.get();
  48.  
  49. return 0;
  50. }

There may well be more elegant ways of doing it!

Cobberas
Reputation Points: 10
Solved Threads: 0
Light Poster
cobberas is offline Offline
28 posts
since Apr 2008
Apr 11th, 2008
0

Re: Dynamic array of structures

why not just cout << ptrBar[0].brand << //next stuff
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,895 posts
since Aug 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: Cant understand random number generation
Next Thread in C++ Forum Timeline: TIC TAC TOE automized player....is me!!





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


Follow us on Twitter


© 2011 DaniWeb® LLC