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 开发者_JS百科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!
You could declare:
Coords *latLng[2];
for the issue "latLng" not being initialized, or malloc it in main
.
For the others - there's no reason for the functions to take the pointer as the first argument at all, since it's not doing anything with the original value. So do:
Coords* AdicionaValores(double x, double y)
{
Coords* v = (Coords*) malloc(sizeof(Coords));
....
}
Quadrado* AdicionaValoresQuadrado(double x1, double y1, double x2, double y2)
{
Quadrado* q = (Quadrado*) malloc(sizeof(Quadrado));
....
}
And then just don't pass that first argument to them in main.
latLng
and q1
are indeed uninitialized.
Coords** latLng;
latLng[0] = …
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 initialize latLng
to 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.
Coords* latLng[2];
latLng[0] = …
After that change, you'll still get warnings about uninitialized variables, but this is due to a problem in the interface of AdicionaValores
and AdicionaValoresQuadrado
. 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 declare v
as a local variable.
Coords* AdicionaValores(double x, double y)
{
Coords *v = malloc(sizeof(Coords));
v->x = x;
v->y = y;
return v;
}
(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 if malloc
runs out of memory, but that's ok for now.) Then in main
:
Coords* latLng[2];
Quadrado* q1;
latLng[0] = AdicionaValores(-23.000490,-43.346687);
latLng[1] = AdicionaValores(-22.988243,-43.342224);
q1 = AdicionaValoresQuadrado(-23.000490,-43.346687,-22.988243,-43.342224);
Coords** latLng;
//...
latLng[0] = AdicionaValores(latLng[0],-23.000490,-43.346687);
latLng[1] = AdicionaValores(latLng[1],-22.988243,-43.342224);
Here you declare a pointer to pointer to Coords
structure(s), but you continue to dereference an uninitialized pointer in the next two lines. At this point, latLng
is not a valid pointer and may have any value.
You are invoking undefined behavior. You need to initialize latLng
before you dereference it based on the number of sub elements (pointers to Coord
) that it will point to. You may then continue to initialize the sub elements, like so:
Coords* latLng[2];
//...
latLng[0] = // some assignment
latLng[1] = // some assignment
Another problem that you may not have realized yet is that your malloc
is not affecting the value of the pointer that you passed into the AdicionaValores
function. You are passing the pointer by value. If you want to assign a new value to the original pointer you will need to pass in a pointer to it, i.e.,
private Coords* AdicionaValores( Coords** q, double x, double y )
{
*v = (Coords*) malloc(sizeof(Coords));
}
This seems to work because you are simply returning the pointer. You do not need to take the first parameter at all because it is never used. Simply omit it and return a new pointer.
精彩评论