how to fill an array that it's part of a struct using a function that can't have an array as arguments

922 Views Asked by At

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?

1

There are 1 best solutions below

2
On

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.