Here is my code:
int main(void)
{
int i;
Coords** latLng;
Quadrado* q1;
latLng[0] = AdicionaValores(latLng[0],-23.000490,-43.346687);
latLng[1] = AdicionaValores(latLng[1],-22.988243,-43.342224);
q1 = AdicionaValoresQuadrado(q1,-23.000490,-43.346687,-22.988243,-43.342224);
printf("# Connecting to database.\n");
for(i=0;i<2;i++)
{
if(clientInside(q1, latLng[i]))
printf("Dentro");
else
printf("Fora");
}
system("PAUSE");
}
Here is AdicionaValores and AdicionaValoresQuadrado:
Coords* AdicionaValores(Coords* v, double x, double y)
{
v = (Coords*) malloc(sizeof(Coords));
v->x = x;
v->y = y;
return v;
}
Quadrado* AdicionaValoresQuadrado(Quadrado* q, double x1, double y1, double x2, double y2)
{
q = (Quadrado*) malloc(sizeof(Quadrado));
q->x1 = x1;
q->x2 = x2;
q->y1 = y1;
q->y2 = y2;
return q;
}
it compiles just fine with 2 warnings, telling me that latLng and q1 are uninitialized! what should I do ?? malloc them on main ? help!
latLngandq1are indeed uninitialized.You’ve never assigned anything to
latLng, and yet you try to treat it as a pointer to an array where you want to put a value. You need not only to initializelatLngto a valid pointer, but also allocate memory for the array. Since this is an array with a fixed size (2) which doesn’t need to last beyond the lifetime of the function, you can allocate it on the stack.After that change, you’ll still get warnings about uninitialized variables, but this is due to a problem in the interface of
AdicionaValoresandAdicionaValoresQuadrado. These functions never actually use their first argument, but they require one, and you’re passing a completely arbitrary value. Just remove the first argument, and declarevas a local variable.(Note that you don’t need to cast the return value of
malloc, and you should never use a cast unless you have a reason to and you understand why. In a production program, you should check ifmallocruns out of memory, but that’s ok for now.) Then inmain: