Jump to content
  • Advertisement
Sign in to follow this  
Starshine_

A very simple question about arrays in C++

This topic is 3650 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! I have one small problem with an array in C++... for example, when I declare an array I can initialize it using an array like this: int SomeKindOfArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; and everything's ok. But when I try to do something like that with an already declared array int SomeKindOfArray[10]; SomeKindOfArray = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; it doesn't let me do that. Is there some way to do it, or do I need to declare and initialize a SomeKindOfArray2 and then copy all the elements to SomeKindOfArray?

Share this post


Link to post
Share on other sites
Advertisement
It's only an initialization syntax. You can't assign like that.

What are you really trying to do? Why can't you just initialize?

If you have a lot of data to put into an array, you should consider reading it in from a file instead.

Share this post


Link to post
Share on other sites
Like Zahlman said: You can't assign values that way afer you declared an array in a previous statement.

If you have a lot of elements you could let it read in with a loop for example and using the assign statement for just one cell:

for (int i ;loop requirements/test(usually i < length of SomeKindOfArray); i++ )
{
SomeKindOfArray = value;

}






If you just have a few values you could do it by hand by doing:


SomeKindOfArray[0] = value;
SomeKindOfArray[1] = value;
SomeKindOfArray[2] = value;
etc.





I hope I did not state the obvious and you already knew this, because seeing the more advanced topics of your recent postings I would assume you would already know this.

Conclusion: You can initialize an array like your first example only in the same line as the declaration. If you want to do it later, you should do it cell by cell like my examples. At least my Java background says so.

Share this post


Link to post
Share on other sites
Thanks for the replies :) Yes, I know that it's an initialization syntax, but I thought that maybe it's possible to do something similar after the array is initialized... I needed that because I have a vertex array which is a field in an object... and in the object constructor I need to put the right data in the array. I can't do it in a for loop because the coordinates of each vertex aren't really arranged in a pattern which can be used with such a loop, and the array is quite big so that doing it by hand like SomeArray[0] = something; SomeArray[1] = something else; ... would require a lot of typing... so, what I did was to initialize a temporary array in the constructor with the right vertex coordinates, and then use memcpy to copy that data to the vertex array which is a field in the object I'm using. It works well :) Maybe for a professional programmer using unneeded temporary arrays isn't really a very elegant solution, but at least it's working and it's doing what I need. :)

Share this post


Link to post
Share on other sites
Quote:
Unfortunately, I can't do that, because the data is generated based on the parameters in the constructor...
Now, I’m a bit confused. If you’re generating the numbers, then you can store the numbers in the array on generation.

Share this post


Link to post
Share on other sites
If you want to post a code snippet, or the actual project, I can better analyze a fix for your problem. Sounds like the placement of assigning the elements can be done more efficiently.

Share this post


Link to post
Share on other sites
Quote:
Original post by oler1s
Quote:
Unfortunately, I can't do that, because the data is generated based on the parameters in the constructor...
Now, I’m a bit confused. If you’re generating the numbers, then you can store the numbers in the array on generation.

Yes, that's what I'm doing. :) I just thought that maybe it's possible to do it in a more efficient way, without creating a temporary array.


Quote:
Original post by jeffreymahmoudi
If you want to post a code snippet, or the actual project, I can better analyze a fix for your problem. Sounds like the placement of assigning the elements can be done more efficiently.


ok... I have an object with a private field declared like this:

GLfloat vertexData[36];

and in the object's constructor I put data in that array:

GLfloat tempArray1[] = {
-2.0f+posx,0.0f+posy,0.0f+posz, -2.0f+posx,2.0f+posy,0.0f+posz, 2.0f+posx,2.0f+posy,0.0f+posz, 2.0f+posx,0.0f+posy,0.0f+posz,
-rotatedx+posx,0.0f+posy,-rotatedz+posz, -rotatedx+posx,2.0f+posy,-rotatedz+posz, rotatedx+posx,2.0f+posy,rotatedz+posz, rotatedx+posx,0.0f+posy,rotatedz+posz,
-rotatedx+posx,0.0f+posy,rotatedz+posz, -rotatedx+posx,2.0f+posy,rotatedz+posz, rotatedx+posx,2.0f+posy,-rotatedz+posz, rotatedx+posx,0.0f+posy,-rotatedz+posz
};
memcpy(vertexData,tempArray1,36*sizeof(GLfloat));


where posx, posy and posz are the constructor's parameters, and rotatedx and rotatedz are variables that are calculated earlier in the constructor. :) The whole program works well, and exactly like I wanted, I was only curious if there's a more efficient way to do it.

Share this post


Link to post
Share on other sites
Why not just assign each value individually? It'll be a lot easier to read and skips the temporary.

e.g.

vertexData[0] = -2.0f+posx;
vertexData[1] = 0.0f+posy;
vertexData[2] = 0.0f+posz;
vertexData[3] = -2.0f+posx;
// etc.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!