Hey, sorry if this question seems basic however I am relatively new to C++ and have done some searching around but to no avail. I then came across this community and thought I would try it out :)

Basically, using Visual C++ 2008, I have a custom data structure, lets call it "MyStructure".

struct MyStructure {
	float x, y, z;
};

I am then creating an array of type 'MyStructure' later on in my code:

MyStructure myArray[100];

Now I know I can modify the array elements using the following syntax...

myArray[0].x = 12.0f;
myArray[0].y = 13.0f;
myArray[0].z = 15.0f;
myArray[1].x = 15.0f;
myArray[1].y = 13.0f;
myArray[1].z = 22.0f;
myArray[2].x = 10.0f;
myArray[2].y = 32.0f;
...
etc

... however some of these arrays are quite large which renders this method massively time consuming.

Is there a fast way of inputting values into each array element?

I tried the following syntax, however it seems to be quite temperamental:

MyScructure myArray = {{12.0f,13.0f,15.0f},{15.0f,13.0f,22.0f},{10.0f,32.0f,44.0f}};

...
later in my code when i need to modify the array
...

myArray = {{132.0f,134.0f,152.0f},{151.0f,133.0f,242.0f},{105.0f,372.0f,944.0f}};

Thanks in advance for any replies.

Recommended Answers

All 2 Replies

You could store the data as a separate file, then the code to read and store the data to your array of structs is quite small and simple.

Your last example is almost correct in line 1 for initializing the array, but you must indicate that it's an array

MyScructure myArray[ ] = {{12.0f,13.0f,15.0f},.....

this way, sufficient number of array elements will be allocated to hold the initializer list.
Line 7 will not work, you cannot assign to an array in that manner. You have to visit each element and make singular assignments, as you show in your first example.

Hi! Thanks for the reply.

Ah yes I see that error I made in the last code snippet where I was not assigning it as an array. That part was correct in my actual code I just re-wrote it wrong.

Thanks for the tip about using a separate file, it never occurred to me, however due to the nature of the program I don't think that is a viable option.

Line 7 will not work, you cannot assign to an array in that manner. You have to visit each element and make singular assignments, as you show in your first example.

I was afraid of this. I'll see if I can find a faster solution :)

Thanks again for the reply.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.