0

Ok, guys, please don't be mean to me, i'm just a girl trying my hand at this coding thing, and being terribly confused by it.

I have this exercise to creat an abstract data type and in one part of it i need to take some values from a file and create a line using them. On the file I have first the number of points in the line then the pair coordinates of each point. The structs i'm using are this:

    typedef struct ponto{
      double x;
      double y;
       } ponto;

    typedef struct linha{
      double numVertices;
      ponto verticesLin[ ];
       }linha;

and the function i need to use is this:

void criaLinha (linha *, double , ponto *);

So i wrote this code to make the transition of the data from the file to the buff and then to the struct linha by using that function:

    BuffToLine(ponto buff[], numMax, linha *l){
      double i;
      ponto Aux;
       for(i=0, i<numMax , i++){
           Aux.x = buff[i].x;
           Aux.y = buff[i].y;
        criaLinha(*l, i, *Aux);
                       }
                      }

    void criaLinha (linha *l, double numVertices, ponto *vertices){

       *l.verticesLin[numVertices].x = Aux.x;
       *l.verticesLin[numVertices].y = Aux.y;
                 }

The problem is, i don't know how to pass the values from the file to the buffer, and i'm not sure the code i wrote will work, because i can't test is without the buffer. So... could someone help me understand how to creat this buffer and if there is a better way of creating the lines with the 'criaLinha' function?

Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • Your declaration of the second struct is wrong. – Uchia Itachi Sep 01 '13 at 16:39
  • What is the problem it does not compile or run ? (and Uchia is right BTW) – dzada Sep 01 '13 at 16:50
  • @UchiaItachi The second structure is *not* wrong if he's using a C99-compliant compiler with support for [*flexible* array members](http://stackoverflow.com/questions/3047530/flexible-array-member-in-c-structure). The use of a `double` for indexing, however, is definitely wrong. – WhozCraig Sep 01 '13 at 17:36
  • It doesn't run, because i need to create a buffer to fill the linha struct with the file data. But i don't know how to do that. The test file is like this: `2 -9 8 5 4 1 4 6` the "1" it the beginig of another line, i don't know how to stop the buffer from getting that too when creating the first line. I hope it's not too confusing. (i've fixed the struct thing, thank you) – user2737566 Sep 01 '13 at 17:51

1 Answers1

0

There is a lot wrong with your code.

First:

ponto verticesLin[ ];

is not valid standard C, gcc will accept this, but if you wish to have a tailing array in a struct then the proper declaration is:

ponto verticesLin[0];

If you do this then you need to have sufficient memory allocated for the struct.

Second:

 double i;

Don't use double type for array indices, use int or size_t.

Third: it is good practice to properly indent your code and give your variables meaningful names otherwise even you won't be able to read it next month.

Now if you declare your structs as

typedef struct ponto {
    double x;
    double y;
} ponto;

typedef struct linha {
    size_t numVertices; // counters should have an integer type
    ponto verticesLin[0];
} linha;

Then you need to allocate sufficient space when you create an instance of linha.

linha * l = malloc(sizeof(l) + numVertices * sizeof(ponto));

When you pass your arguments you also need to get your types right:

void criaLinha (linha * l, size_t numVertices, ponto * vertices);

BuffToLine(ponto buff[], size_t numMax, linha * l) {
    size_t i; // should be an integer type
    ponto Aux;
    for(i=0, i< numMax , i++){
        Aux = buff[i]; // you can copy the whole thing, no need to to it by variable
        criaLinha(
            l // l is already a pointer no need to dereference it
            , i
            , &Aux // Aux is a ponto, you need a ponto*, so you need to take the address of it
        );
    }
}

Hope this helps a little bit, you didn't provide enough code to help you with more.

Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • Thanks that did help a bit. But i can't change the types of the argument of the function `criaLinha`.The whole code should be an ADT to creat points,lines and polygons using coordenates given in a file. I should implement certain functions that were given using the types in the argument. So if the file is like this 5 -6 7 the first number is the amount of points in the line and the others are the points. I need to use that to create `linha l` using the function criaLinha(linha*l, numVertice, ponto*). If they need to be pointers, maybe i should move the function out of the other `BuffToLine`? – user2737566 Sep 01 '13 at 17:34
  • You should note his decl for the `ponto verticesLin[];` member is legitimate if he is using a C99-compliant compiler. Its called a [flexible array member](http://stackoverflow.com/questions/3047530/flexible-array-member-in-c-structure). – WhozCraig Sep 01 '13 at 17:38