Thread: Problem with arrays, pointers and functions when combined

  1. #1
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92

    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?

    Thanks in advance

    The Wazaa

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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. #3
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 06:48 PM
  2. Problem with pointers and functions
    By Kheila in forum C++ Programming
    Replies: 5
    Last Post: 10-13-2005, 12:40 PM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Pointers, arrays , functions
    By sballew in forum C Programming
    Replies: 19
    Last Post: 09-16-2001, 11:12 PM