I need some ideas on my array of struct implementation. This is what I have in my structs. I plan on making SHAPES an array because I will have multiple SHAPES. Each shape will have multiple x and y coordinates. Because of this I'm not sure if should make a linked list or not.

struct START
{
    int x; 
    int y;
};
struct END
{
    int x; 
    int y;
};
struct SHAPES
{
    int x [100]; 
    int y [100];
    int *link;
};
struct DISTANCE
{
    int distance_traveled; 
    int distance_to_finish;
};

I was reading this and was wondering if I needed to malloc or calloc my structs as I create them. If so why and if not why? Malloc and calloc are always confusing to me.

http://stackoverflow.com/questions/10468128/how-do-you-make-an-array-of-structs-in-c/24542015#24542015

int main(int argc, char *argv[])
{

    //Is it better to make these pointers? 
    struct START start;
    struct END end;
    struct SHAPES shapes[100];

    while(fgets(line, 80, fp) != NULL)
    {
        //I have a very annoying issue here, I don't know the size of the lines. 
        //If there are more values I want to increment the x[] and y[] array but NOT the 
        //shapes array. I can only think of one way to deal with this by putting a bunch of 
        //variables in the sscanf. I discovered putting sscanf on the next line didn't do what 
        //I was hoping for. 
        sscanf(line, "%d%*c %d%*c ", &shapes[i].x[i] , &shapes[i].y[i] );
        printf(" line is: %s \n", line);
        sscanf(line, "%d%*c %d%*c ", &x1_main , &y1_main );
        printf(" line is: %s \n", line);
        printf(" shapes[i].x[i] is: %d \n", shapes[i].x[i]);
        printf(" shapes[i].y[i] is: %d \n", shapes[i].y[i]);
        printf(" x1_main is: %d \n", x1_main);
        printf(" y1_main is: %d \n", y1_main);
        i++;
        memset(line, 0, 80);
    }

}

This is what my file looks like. Adding the %*c seemed to handle the commas and semicolons appropriately.

10, 4
22, 37
22, 8; 2, 0; 3, 6; 7, 8; 5, 10; 25, 2
1, 2

I got that idea from here.

https://www.daniweb.com/software-development/c/threads/334515/reading-comma-separated-values-with-fscanf

Recommended Answers

All 3 Replies

your logic must be correct , i h'v completed c++ recently but me here is still not sure about your code but try compiling step by step or divide your problem into half portion and check result , hope you will get your result ....

First off, I would recommend that, rather than having individual named structures as one-off types, that you define a POINT struct type and use that as the basis of your larger data structures:

struct POINT
{
    double x; 
    double y;
};

 /* a vector represents a directional line segment, that is,
  * a line segment going from one point to another with a
  * specific direction.  You can get the length of a vector by
  * the Pythagorean theorem: 
  * len = sqrt(square(end.x - start.x) + square(end.y - start.y))  
  */
struct VECTOR
{
    struct POINT start, end;
};

/* FACE is a doubly-linked ring structure of VECTORs
 * this means that each FACE points to the face before
 * it and the face after it, with the last face pointing
 * to the first one and vice versa.
 */
struct FACE
{
    struct VECTOR face;
    struct FACE *prev, *next; 
};

typedef SHAPE struct FACE*;    /* pointer to an arbitrary face */ 

struct SHAPE_LIST 
{
    SHAPE shape;
    struct SHAPE_LIST* next;
}

You will want functions for the following purposes: to create a vector from two points; to create a shape given a list of points (including remembering to close it); to compute the size of a given vector (I already gave you the formula for that above); to count the number of faces in a shape, given the shape (you would need to take the starting face and move around the shape until you reach it again); and to get perimeter of the shape by adding the vector lengths. That's just the basics, you'll need more than just those, but that's a start.

Sorry, I just realized I didn't answer your actual question; my apologies. As for whether you need to dynamically allocate the memory, in the case of a linked list, then answer is yes, both for the list itself and for any elements held by the list. So, if you use the approach I gave, earlier, for a given shape of size n, you would first need to allocate a SHAPE_LIST node, then allocate n FACE nodes. You would not need to explicitly allocate the VECTOR for each FACE, or the POINTs in each VECTOR, since those are values rather than pointers to values, but they would get allocated when the FACE is allocated.

I also forgot to explain why I changed the POINT elements from int to double. Mostly, it is because integer division is problematic when getting the vector sizes (which often have to be floating-point anyway, since they involve square roots), but also because not all common shapes are easily fit into integral positions. Using doubles just avoids certain problems, and while it can lead to other problems, it's generally easier to deal with in this case.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.