Assigning to members of elements in a dynamic array of structures ?

Hello,

I've been working from a textbook's exercises where one question is creating a dynamic array of structures and would appreciate any help.

If the structure variable is called sample and the array is to be called keyboard

struct sample{
char brand[20];
char specs[20];
float price;
}

Declaring an array of 10 elements in main would be as:

sample * plants = new sample [10];

My questions are:

Does there need to be a number of elements as shown in the square brackets, i.e. can it be left empty?

How would values be assigned to members of the structures in the array, especially the char arrays and how can it be done using curly brackets, filling all three member of each structure ?

Thank you
Change your structure to something like this:
1
2
3
4
5
6
7
8
9
10
11
struct sample{
char brand[20];
char specs[20];
float price;
void	assignAll(char brand[20],char specs[20],float price)
{
  strcpy(this->brand,brand);
  strcpy(this->specs,specs);
  this->price=price;
}
};

, and use:
plants[0].assignAll("lalala","tatata",3.14);
Hi,

Does there need to be a number of elements as shown in the square brackets, i.e. can it be left empty?

Where?
In structure. Yes. This need to be known in compilation time if you want to use array of chars.

In sample * plants = new sample [10];

Does there need to be a number
No. There can be variable also.

can it be left empty
No. It would be syntax error

But imo std::string would be better choice in your situation
Last edited on
Does there need to be a number of elements as shown in the square brackets, i.e. can it be left empty?

It cannot be left empty.


How would values be assigned to members of the structures in the array, especially the char arrays and how can it be done using curly brackets, filling all three member of each structure ?

In C++11, you might do the following:

1
2
3
4
5
6
7
8
9
10
11
struct sample {
	char brand[20];
	char specs[20];
	float price;
};

int main()
{
	sample* plants = new sample[10];
	plants[0] = { "mamama", "dadada", 3.14f };
}


Although, I would suggest using strings over character arrays (and eschewing manual memory management.)

Last edited on
@cire
plants[0] = { "mamama", "dadada", 3.14f };

That will not compile. I should be something like this.
plants[0] = sample{ "mamama", "dadada", 3.14f };
@stryku:
Off the cuff code ran only through VC++ which accepts it as written. Apologies. ;)
Hi thanks a lot to everyone for replying.

skaa, is the void assignAll in the struct a type of constructor ?

stryku thanks for pointing out that the struct variable should be before the {} for each array element, I compile with tdm-gcc.
Is this only necessary for dynamic arrays and not for static ones ?

Thanks again.
No, it is not a constructor because it does not create a new object of the structure. The constructor will look like:
1
2
3
4
5
6
7
8
9
struct sample
{
...
  sample()
  {
    price=3.33;
  }
...
}

(same name as the structure).
Is this only necessary for dynamic arrays and not for static ones ?

Is what necessary? What are you referring to here?
plants[0] = { "mamama", "dadada", 3.14f }; works in clang too: http://coliru.stacked-crooked.com/a/f015c6c0cdec0348 and also in the intel compiler

I think it relies on a fairly recent defect report resolution against C++14 (CWG 1467) which corrects aggregate-initialization to correctly initialize array members from string literals, hopefully gcc will catch up (I submitted https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67259 )
Last edited on
MikeyBoy, I was referring to adding the name of the struct variable, sample, in front of the {, when filling each element.

i.e. plants[0] = sample{"keyboard", "unicode, wireless", 29.99};

Because gcc gives an error if sample, which is the type of struct, isn't included. Please read stryku's 8:55pm post.

Thank you all again for the replies.
What that's doing is creating a nameless temporary object of type sample, and then copying the value of that nameless temporary to plants[0].

Using it has nothing to do with whether it's a dynamic array.
Topic archived. No new replies allowed.