943,163 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1647
  • C++ RSS
Mar 13th, 2010
0

Array of custom type - quick syntax query.

Expand Post »
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".

C++ Syntax (Toggle Plain Text)
  1. struct MyStructure {
  2. float x, y, z;
  3. };

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

C++ Syntax (Toggle Plain Text)
  1. MyStructure myArray[100];

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

C++ Syntax (Toggle Plain Text)
  1. myArray[0].x = 12.0f;
  2. myArray[0].y = 13.0f;
  3. myArray[0].z = 15.0f;
  4. myArray[1].x = 15.0f;
  5. myArray[1].y = 13.0f;
  6. myArray[1].z = 22.0f;
  7. myArray[2].x = 10.0f;
  8. myArray[2].y = 32.0f;
  9. ...
  10. 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:

C++ Syntax (Toggle Plain Text)
  1. MyScructure myArray = {{12.0f,13.0f,15.0f},{15.0f,13.0f,22.0f},{10.0f,32.0f,44.0f}};
  2.  
  3. ...
  4. later in my code when i need to modify the array
  5. ...
  6.  
  7. 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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aatwo is offline Offline
4 posts
since Mar 2010
Mar 13th, 2010
0
Re: Array of custom type - quick syntax query.
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
C++ Syntax (Toggle Plain Text)
  1. 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.
Reputation Points: 1268
Solved Threads: 228
Posting Virtuoso
vmanes is offline Offline
1,894 posts
since Aug 2007
Mar 13th, 2010
0
Re: Array of custom type - quick syntax query.
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.

Quote ...
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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aatwo is offline Offline
4 posts
since Mar 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: What is a pointer and memory allocation?
Next Thread in C++ Forum Timeline: c++ questions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC