2 Replies - 579 Views - Last Post: 13 October 2014 - 02:37 AM Rate Topic: -----

#1 COKEDUDE   User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 274
  • Joined: 01-February 10

Array of struct implementation in C

Posted 13 October 2014 - 12:15 AM

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...542015#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....ues-with-fscanf

Is This A Good Question/Topic? 0
  • +

Replies To: Array of struct implementation in C

#2 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1271
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Array of struct implementation in C

Posted 13 October 2014 - 01:45 AM

I'm assuming the file represents a set a vertices for each shape. If so, I would create a struct called Vertex to represent each vertex, rather than using parallel arrays.

struct Vertex {
   int x, y;
};



Since you don't know how many vertices a shape may have, a linked list may be the most memory efficient method.

e.g.
struct VertexLinkedList {
   //implement
};

struct Shape {
   VertexLinkedList *vertices;

   //etc..
}




Quote

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.


Its up to you, malloc simply allocates memory, calloc allocates memory at sets all the bytes to zero (equivalent to a malloc then memset).

For parsing the vertex coordinates, take a look at the function strtok

This post has been edited by jjl: 13 October 2014 - 01:46 AM

Was This Post Helpful? 0
  • +
  • -

#3 COKEDUDE   User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 274
  • Joined: 01-February 10

Re: Array of struct implementation in C

Posted 13 October 2014 - 02:37 AM

View Postjjl, on 13 October 2014 - 02:45 AM, said:

I'm assuming the file represents a set a vertices for each shape. If so, I would create a struct called Vertex to represent each vertex, rather than using parallel arrays.

struct Vertex {
   int x, y;
};



Since you don't know how many vertices a shape may have, a linked list may be the most memory efficient method.

e.g.
struct VertexLinkedList {
   //implement
};

struct Shape {
   VertexLinkedList *vertices;

   //etc..
}




Quote

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.


Its up to you, malloc simply allocates memory, calloc allocates memory at sets all the bytes to zero (equivalent to a malloc then memset).

For parsing the vertex coordinates, take a look at the function strtok


The first 2 lines are special cases. They are my starting point and ending point. I will be creating a search algorithm after I properly make my shapes. Each line represents a new shape. That was the purpose of my shapes[100] array. I have no idea how many sets of vertices each shape will have, that was the purpose of x [100] and y [100]. I could have a simple shape like a square or I could have a complex shape like a star. Is there a way to get strtok to output as int? Every example I saw was with char. Would I need atoi to convert it?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1