Jump to content

Help with using new to create dynamic array of structures

- - - - -

  • Please log in to reply
6 replies to this topic

#1
Vince

Vince

    Newbie

  • Members
  • PipPip
  • 11 posts
Hi guys.
I need a bit of help as I have just started learning c++
Currently, I have this.


#include "stdafx.h"

#include <iostream>

int main()


{

	using namespace std;


	struct CandyBar

	{

		char brand[20];

		double weight;

		int calories;

	};


	CandyBar *pt = new CandyBar [3];

	pt = &pt[0];

	*pt = { "Nestle", 2.5, 632 };	

	cout << "Brand: " << pt->brand << endl << "Weight: " << pt->weight << endl << "Calories: " << pt->calories << endl;



	pt = &pt[1];

	*pt = { "Nestle", 2.5, 632 };

	cout << "Brand: " << pt->brand << endl << "Weight: " << pt->weight << endl << "Calories: " << pt->calories << endl;


	

	pt = &pt[2];

	*pt = { "Nestle", 2.5, 632 };

	cout << "Brand: " << pt->brand << endl << "Weight: " << pt->weight << endl << "Calories: " << pt->calories << endl;


	delete [] pt;


	cin.get();	

	return 0;

}


Unfortunately, it isn't seem to be working and I am not sure if what I am doing is correct either.
It would be great if someone would help.
Cheers.

#2
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
I don't know why you are assigning these statements
pt = &pt[0];
pt = &pt[1];
pt = &pt[2];
These statements are not necessary.
since you have used already declared pt to point array of structures of size 3. you can just access the variables like pt[0], pt[1], pt[2]. Its just like C pointer to an array.
here is the code:
CandyBar * pt = new CandyBar[3];
    pt[0].brand = "Nestle";
    pt[0].calories = 632;
    pt[0].weight = 2.5;

     // likewise do it for pt[1] and pt[2]

Note: convert the
char brand[20];
into
char *brand;
Or else while you are assigning
pt[0].brand = "Nestle";
it will prompt an error of typecasting.

#3
Vince

Vince

    Newbie

  • Members
  • PipPip
  • 11 posts
Thank you so much.
I just have one question. Why do we convert the
char brand[20];
into
char *brand;
Again, thanks!

#4
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
There is no specific reason for changing it.

if you use
char brand[20];
and
pt[0].brand = "Nestle";
the compiler will generate C2440 error.

you can use either way.

if you use
char *brand;
it will be easier to assign. just
pt[0].brand = "Nestle";

But if you use
char brand[20];
you have to do strcpy
strcpy(pt[0].brand, "Nestle");


#5
Vince

Vince

    Newbie

  • Members
  • PipPip
  • 11 posts
Thank you for your help. It is greatly appreciated.

#6
Firebird_38

Firebird_38

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
Just so you know:

 
pt = &pt[0];
pt = &pt[1];
pt = &pt[2];

This is not only "not necessary" it's wrong. What you are doing is, first line, nothing, then you're changing pt to its second element. You're still going to be alright now.
But then you're picking the 3rd element of the current pt, which is now the second element. So, your pt is now pointing to the 4th element, which is element number 3.

In esence, you should be using a different var to assign to, so you don't overwrite your original pt.

As in:

 
pt_temp = &pt[0];
pt_temp = &pt[1];
pt_temp = &pt[2];

And then use pt_temp instead of pt to access your elements. This is then okay.

Edited by Firebird_38, 20 April 2010 - 07:24 AM.


#7
Vince

Vince

    Newbie

  • Members
  • PipPip
  • 11 posts
Thanks for your help also :)
Posted Image




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users