mardi 30 juin 2015

Dynamic array's in C


I am trying to dynamically create an array of rectangles with random values. However when trying to access the space I malloc I get a seg fault.

My Code:

typedef struct Point
{
    double x;
    double y;
} PointT;

typedef struct Rect
{
    PointT location;
    char color;
    double w; //width
    double h; //height
} RectT;
main()
{
    RectT *recs;
    randRect(&recs);
}
void randRect(RectT **rects)
{
    int i;
    rects =malloc(numRec*sizeof(RectT*));
    for(i = 0; i < numRec-1; i++)
    {
          rects[i]->w=rand()%20;
          rects[i]->h=rand()%20;
          rects[i]->location.x=rand()%20;
          rects[i]->location.y=rand()%20;
    }
}

numRec is defined as 50


Aucun commentaire:

Enregistrer un commentaire