954,124 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Creating dynamic array structures

I am learning C++ using Stephen Pratas "C++ Primer Plus" (excellent book btw). However i am stuck on exercise 6 chapter 4, where in essence i just have to use new to allocate the array of structures dynamically. I can allocate a single structure dynamically, however i still do not know how to initialise all the members at one time, only each member seperately by using cin e.g. i can only do:

cin.get(structure1->name, 20);

and i do not know how to initialise a whole dynamically allocated structure like i can with a normal 1 e.g. i cant do this:

structure1 object1 =
{
"Balh blah blah".
1.88.
5
}

Can please someone help. thnx in advance

Lippy
Newbie Poster
1 post since Apr 2004
Reputation Points: 11
Solved Threads: 0
 

structure object1 = {1,2,3,4,5,6,etc};
structure object2={"something","anything", "another thing"};

Now if u wanted "something" from object 2 to correspond to a number from object one you can use parralell arrays.

But a matrix would be better.

BountyX
Posting Whiz in Training
230 posts since Mar 2004
Reputation Points: 28
Solved Threads: 9
 

Ok--try this (i'm not sure it's perfect...13 yr olds can make mistakes)

#include
#include
#include
struct nod
{
int info;
nod *adr;
};
nod *vf,*sf;// *vf-is the the first element in the list
// *sf- is the last element
void add(int val,nod* &vf)
{
if (!vf) //lista este goala
{
vf=(nod*)malloc(sizeof(nod));
vf->info=val;
sf=vf;
}
else
{
nod *c=(nod*)malloc(sizeof(nod));
c->info=val;
c->adr=NULL;
sf->adr=c;
sf=c;
}
}
void list(nod *vf)
{
nod *c;
c=vf;
while (c)
{
printf("%i ",c->info);
c=c->adr;
}
}
/*void insert(int vc,int vi,nod *vf)
{
nod *c;
c=vf;
while (c->info!=vc) c=c->adr;
nod *e=(nod*)malloc(sizeof(nod));
e->info=vi;
e->adr=c->adr;
c->adr=e;
}*/ //it inserts a nod in the list after looking for a value-vc
// you don't need the insert(...) function
void main()
{
clrscr();
int n;//it reads the numer of elements which will be in the list
printf("n=");scanf("%i",&n);
for (int i=0;i

Fili
Light Poster
34 posts since Jun 2004
Reputation Points: 16
Solved Threads: 0
 

Not sure I understand the request, but using new for dynamic arrays goes like this:

void CreateArray( int numberToCreate )
{
// this allocates space and calls the constructor on each element.
AnObject* theDynamicArray = new AnObject[ numberToCreate ];

// example of use
for (int i = 0; i < numberToCreate; i++)
DoSomething( theDynamicArray[ i ] ); // gets passed as AnObject, not AnObject*

delete [] theDynamicArray; // destructor of each element gets called.
}

The constructor would be responsible for initializing the object, of course, but if you want to load some initial data in from the console or a resource or something, you'd have to do that post-construction. Like, in the DoSomething() routine. Also, a routine like DoSomething() should take AnObject as a reference so it can be changed (and so it doesn't need to be copied by the compiler).

Chainsaw
Posting Pro in Training
436 posts since Jun 2004
Reputation Points: 36
Solved Threads: 11
 

I'm studying C++ using the same book and I found myself in a bit of a jam with that particular exercise. I sorted that out and I'm posting my source code so you may continue with the following chapters. If someone can come up with a nicer or more elegant solution please post.

#include
using namespace std;

int main()
{
struct CandyBar //struct declaration
{
char *brand;
float weight;
int calories;
};

//dynamic array declaration
CandyBar *Candies = new CandyBar[3];
CandyBar *CandyPointer = &Candies[0]; //pointer to a CanyBar data object

//structure initialization one by one
(*CandyPointer).brand = "Mocha Munch";
CandyPointer->weight = 2.3;
CandyPointer->calories = 350;

CandyPointer = &Candies[1];
(*CandyPointer).brand = "Chilly Willy";
CandyPointer->weight = 2.2;
CandyPointer->calories = 200;

CandyPointer = &Candies[2];
(*CandyPointer).brand = "Lemon Rush";
CandyPointer->weight = 2.1;
CandyPointer->calories = 200;

//data output to the screen

//first element of the array
CandyPointer = &Candies[0];
cout << "Name of Candy Bar: " << (*CandyPointer).brand << endl;
cout << "Weight of Candy Bar: " << CandyPointer->weight << endl;
cout << "Number of calories in Candy Bar: " << CandyPointer->calories << "\n\n";

//second element of the array
CandyPointer = &Candies[1];
cout << "Name of Candy Bar: " << (*CandyPointer).brand << endl;
cout << "Weight of Candy Bar: " << CandyPointer->weight << endl;
cout << "Number of calories in Candy Bar: " << CandyPointer->calories << "\n\n";

//third element of the array
CandyPointer = &Candies[2];
cout << "Name of Candy Bar: " << (*CandyPointer).brand << endl;
cout << "Weight of Candy Bar: " << CandyPointer->weight << endl;
cout << "Number of calories in Candy Bar: " << CandyPointer->calories << "\n\n";

cin.get();
return 0;
}

RootUser001
Newbie Poster
1 post since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

>I'm studying C++ using the same book and I found myself in a bit of a jam with that particular exercise.
This thread is two *years* old. If you have a question, start a new thread rather than resurrect one that's already begun to decay. We can't stand the smell of necrothreads.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You