# Thread: Problem with arrays, pointers and functions when combined

1. ## Problem with arrays, pointers and functions when combined

Hi
I've written a particle engine, and decided to try to simulate a cloth. Mostly because I want to test the speed of C++ since I'm used to Flash but I also want to see if it's possible to use it in a game. Anyway, I got an array of particles and I want to pass that to a function which creates a cloth of them. Here's the code:

Code:
void createCloth(int numOfParticles, cParticle* particles[])
{

cParticle* tPart;

for (int i=0; i < (int)sqrt((float)numOfParticles); i++)
{
for (int j=0; j < (int)sqrt((float)numOfParticles); j++)
{
tPart = particles[10*i+j];
tPart->init(j/10, i/10, 0, 0, 0, 0, 0.5);
}
}
for (int i=0; i < (int)sqrt((float)numOfParticles); i++)
{
for (int j=0; j < (int)sqrt((float)numOfParticles); j++)
{
tPart = particles[10*i+j];

if (j != 0)	tPart->addConstraint(&particles[i+j-1], 0.1, false);
if (j!= (int)sqrt((float)numOfParticles)-1) tPart->addConstraint(&particles[i+j+1], 0.1, false);
if (i != 0) tPart->addConstraint(&particles[10*i+j-10], 0.1, false);
if (i != (int)sqrt((float)numOfParticles)-1) tPart->addConstraint(&particles[10*i+j+10], 0.1, false);
}
}
}

//Heres the call for it, along with the array (in winMain function):

cParticle particles[numParticles];
createCloth(numParticles, &particles);
I get the error:

error C2664: 'createCloth' : cannot convert parameter 2 from 'cParticle (*__w64 )[100]' to 'cParticle *'

I've tried anything I can think of, but nothing works. Anyone knows what might be wrong?

The Wazaa

2. Arrays are always passed as pointers, so there is no need to point to the array just to be able to update it.

So
void createCloth(int numOfParticles, cParticle particles[])

And
createCloth(numParticles, particles);

> for (int i=0; i < (int)sqrt((float)numOfParticles); i++)
Just do the sqrt() once, not each time round every loop.

> tPart = particles[10*i+j];
Did you flatten the array because you don't know how to pass a 2D array to a function?

3. Thanks

And.. hmm, don't know why I did like that ^^ Maybe because I wrote it in another way at first and it was easier to do like this then.

Now I just discovered that my physics isn't as good as it's supposed to But that will soon be fixed.

Thanks again

The Wazaa