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?
There is a lot wrong with your code.
First:
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:If you do this then you need to have sufficient memory allocated for the struct.
Second:
Don't use
double
type for array indices, useint
orsize_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
Then you need to allocate sufficient space when you create an instance of
linha
.When you pass your arguments you also need to get your types right:
Hope this helps a little bit, you didn't provide enough code to help you with more.