0

I have some code like (I've simplified it a bit):

# define NUMBER_OF_PARTICLES 1

typedef struct {
    Axes velocity;      // struct with x,y,z floats
} Particle;

Particle * array_of_particles;

Particle create(Particle p) {
    p.velocity.x = 0.0f;
    p.velocity.y = 0.0f;
    p.velocity.z = 0.0f;

    return p;
}

void create_particles() {
    array_of_particles = (Particle *) malloc(sizeof(Particle) * NUMBER_OF_PARTICLES);

    int p;      
    for (p = 0; p < NUMBER_OF_PARTICLES; p++) { 
        Particle current_particle = array_of_particles[p];
        array_of_particles[p] = create(current_particle);
    }
}

Hopefully you can see that I am trying to make the array element at index p be the struct of the current_particle. I think I am misunderstanding how to do this as it returns 0 when I print array_of_particles[p]. Could someone guide me as to the correct way of achieving this?

dgBP
  • 1,681
  • 6
  • 27
  • 42
  • What's actually wrong with that code ? – Jabberwocky Dec 05 '13 at 11:44
  • 1
    [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169). – unwind Dec 05 '13 at 12:09
  • @unwind oh I had noticed this when looking at examples online, but it hadn't really clicked at the difference between the forms. Thanks for pointing it out. – dgBP Dec 05 '13 at 12:20

1 Answers1

4

try

void create_particles() 
{
  array_of_particles 
    = (Particle *) malloc(sizeof(Particle)*NUMBER_OF_PARTICLES);

  int p;      
  for (p = 0; p < NUMBER_OF_PARTICLES; p++) { 
    Particle* current_particle = array_of_particles + p;
    create(current_particle);
  }
}

and change

void create(Particle* p) 
{
  p->velocity.x = 0.0f;
  p->velocity.y = 0.0f;
  p->velocity.z = 0.0f;
}

what you did was to pass a copy of the argument to the function so the changes never left the function.

there is also no need to return the Particle and then copy it, you already are passing the struct to the function so you can modify it by using the argument 'p'.

AndersK
  • 35,813
  • 6
  • 60
  • 86
  • ahhh so I reference the particle and then create pointers to it. Cool, thanks! – dgBP Dec 05 '13 at 11:45
  • I get this error: `error: incompatible types when initializing type ‘struct Particle *’ using type ‘Particle’` at this line: `Particle* current_particle = array_of_particles[p];` – dgBP Dec 05 '13 at 11:49
  • 1
    Try this, Particle current_particle = array_of_particles[p]; create(&current_particle); – Premsuraj Dec 05 '13 at 11:50
  • @claptrap can you explain how the `array_of_particles + p` works? – dgBP Dec 05 '13 at 12:10
  • It's the address of entry p in your array (base address plus p times the size of a Particle because it's a Particle*). Another way of writing it would be &array_of_particles[p]. – Mikkel K. Dec 05 '13 at 12:24
  • @dpBP the `array_of_particles` is the address of the first entry of your array of structs i.e. at index 0. The compiler knows from the pointer type how large your structs are so adding an offset 1,2,.. to the starting address gives a pointer to subsequent entries in your array. essentially writing `array_of_particles + p` is another way of writing `&array_of_particles[p]` – AndersK Dec 05 '13 at 12:31