Dynamic array of structures

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 12
Reputation: cobberas is an unknown quantity at this point 
Solved Threads: 0
cobberas cobberas is offline Offline
Newbie Poster

Dynamic array of structures

 
0
  #1
Apr 10th, 2008
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)

  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
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
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.
  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 449
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 70
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Dynamic array of structures

 
0
  #2
Apr 10th, 2008
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]
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Dynamic array of structures

 
0
  #3
Apr 10th, 2008
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:
  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.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: Dynamic array of structures

 
0
  #4
Apr 10th, 2008
Originally Posted by cobberas View Post
  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++
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 12
Reputation: cobberas is an unknown quantity at this point 
Solved Threads: 0
cobberas cobberas is offline Offline
Newbie Poster

Re: Dynamic array of structures

 
0
  #5
Apr 10th, 2008
That's great - thanks heaps.

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

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 :-)

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.

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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,473
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Dynamic array of structures

 
0
  #6
Apr 10th, 2008
>>I think this will be possible in the next version of C++
You can do that now

  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 12
Reputation: cobberas is an unknown quantity at this point 
Solved Threads: 0
cobberas cobberas is offline Offline
Newbie Poster

Re: Dynamic array of structures

 
0
  #7
Apr 10th, 2008
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:

  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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: Dynamic array of structures

 
0
  #8
Apr 11th, 2008
why not just cout << ptrBar[0].brand << //next stuff
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC