In: Computer Science
Below is part of my code to sort table of x,y,z cordinates using x,y with qsort. I can read all the data into *xyz part of the structure and allocate memory, I keep getting errors on line 7, "read access violation". I would take any help...Thanks
typedef struct
{
int x;
int y;
int z;
} Coordinates;
typedef Coordinates * xyz;
int compare(const void *p1, const void *p2)
{
const xyz num1 = *(const xyz *)p1;
const xyz num2 = *(const xyz *)p2;
7 if (num1->x < num2->x) //read access violation
{
return -1;
}
else if (num1->x > num2->x)
{
return 1;
}
else
{
if (num1->y < num2->y)
{
return -1;
}
else if (num1->y > num2->y)
{
return 1;
}
else
{
return 0;
}
}
Main
{
//allocates memory
Coordinates * xyz = (Coordinates *) malloc(sizeof(Coordinates) * numElem);
qsort(xyz, numElem, sizeof(xyz), compare);
}
If you have any doubts, please give me comment...
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int x;
int y;
int z;
} Coordinates;
int compare(const void *p1, const void *p2)
{
const Coordinates *num1 = (const Coordinates *)p1;
const Coordinates *num2 = (const Coordinates *)p2;
if (num1->x < num2->x) //read access violation
{
return -1;
}
else if (num1->x > num2->x)
{
return 1;
}
else
{
if (num1->y < num2->y)
{
return -1;
}
else if (num1->y > num2->y)
{
return 1;
}
else
{
return 0;
}
}
}
int main()
{
//allocates memory
Coordinates *xyz = (Coordinates *)malloc(sizeof(Coordinates) * numElem);
qsort(xyz, numElem, sizeof(Coordinates), compare);
return 0;
}