views:

160

answers:

1

I am trying to build an application to simulate some basic spheres moving around for this assignment I have got.

I am relatively new to C but I have been using .NET and java for some time.

The problem that i am facing is that it doesn't look like the data is being assigned to the array outside of the init statement when i actually need it. Is this somethingt to do with the way i've declared the array containing the particles.

I want to create an array of structs which can be accessed from various methods so at the top of my file below the include statements I have used:

struct particle particles[];


// Particle types
enum TYPES { PHOTON, NEUTRINO };

// Represents a 3D point
struct vertex3f
{
    float x;
    float y;
    float z;
};

// Represents a particle
struct particle
{
    enum TYPES type;
    float radius;
    struct vertex3f location;
};

I have an initialise method which creates the array and assigns particles to it

void init(void)
{
    // Create a GLU quadrics object
    quadric = gluNewQuadric();
    struct particle particles[max_particles];

    float xm = (width / 2) * -1;
    float xp = width / 2;
    float ym = (height / 2) * -1;
    float yp = height / 2;

    int i;
    for (i = 0; i < max_particles; i++)
    {
        struct particle p;

        struct vertex3f location;
        location.x = randFloat(xm, xp);
        location.y = randFloat(ym, yp);
        location.z = 0.0f;

        p.location = location;
        p.radius = 0.3f;

        particles[i] = p;        
    }
}

and then a inside a draw method a method to draw the set scene

// Draws the second stage
void drawSecondStage(void)
{

    int i;

    for (i = 0; i < max_particles; i++)
    {
        struct particle p = particles[i];

        glPushMatrix();
        glTranslatef(p.location.x , p.location.y, p.location.z );
        glColor3f( 1.0f, 0.0f, 0.0f );
        gluSphere( quadric, 0.3f, 30, 30 );
        glPopMatrix();

        printf("%f\n", particles[i].location.x);
    }
}

here's a copy of my code http://pastebin.com/m131405dc

Thanks in advanced!

+5  A: 

The problem is this definition:

struct particle particles[];

It's not reserving any memory, just defining an empty array. You need to put something in those square brackets. It's a wonder all your writing to various positions in this array haven't caused segfault crashes ...

You could try with max_particles, although I'm not sure using a variable (albeit a const one) is legal in a C definition.

The classic solution is to use the preprocessor, like so:

#define MAX_PARTICLES 50

struct particle particles[MAX_PARTICLES];

And then use MAX_PARTICLES in the various loops. I suggest instead putting the literal in the brackets:

struct particle particles[50];

And then writing loops like this:

for(i = 0; i < sizeof particles / sizeof *particles; i++)

That is a compile-time division, so it's not costing you anything, and you're re-using the definition itself to provide the number of elements in the array, which (IMO) is elegant. You could of course go some middle way and define a new macro, like so:

#define MAX_PARTICLES  (sizeof particles / sizeof *particles)
unwind
Thank you so much for your answer - and going into the depth you did. Thank you!
Malachi