I have an array of preallocated structs. I am attempting to construct a function which uses that array as input so that I can construct an array of pointers to preallocated struct array. I then want to sort the pointer array using qsort, but I seem to be misunderstanding how the pointers are passed around as when I try to run my code, it is a minefield of memory access violations.
The first issue seems to be with the line:
(&(pRet->ppIndexArray))[i] = &pTestElement[i];
in sortedIndexPointer my thinking was that ppIndexArray is a pointer to an array of pointers, and I need to get the address of the array pointed to by ppIndexArray and then write the address of the current TestElement to that, but that seems to not be right.
Please see my simplified code below:
#include <stdlib.h>
#include <stdio.h>
typedef int(*CompareFunction)(const void *, const void *);
typedef struct TestElement
{
const char *pName;
double data;
} TestElement;
typedef struct TestElementIndex
{
unsigned elementCount;
TestElement **ppIndexArray;
} TestElementIndex;
int CompareNumbers(const void *p1, const void *p2) {
TestElement *pTest1 = *(TestElement **)p1;
TestElement *pTest2 = *(TestElement **)p2;
if (pTest1->data > pTest2->data) {
return 1;
}
else if (pTest1->data < pTest2->data) {
return -1;
}
else {
return 0;
}
}
TestElementIndex *sortedIndexPointer(TestElement *pTestElement, const unsigned Count,
CompareFunction comparer) {
TestElementIndex *pRet = malloc(sizeof(TestElementIndex));
pRet->elementCount = Count;
pRet->ppIndexArray = malloc(sizeof(TestElement *)*Count);
for (unsigned i = 0; i < Count; i++) {
(&(pRet->ppIndexArray))[i] = &pTestElement[i];
}
if (comparer) {
qsort(pRet->ppIndexArray, sizeof(TestElement *), Count, comparer);
}
return pRet;
}
void DisplayElements(const TestElementIndex *pTestElementIndex) {
for (unsigned i = 0; i < pTestElementIndex->elementCount; i++) {
printf("%lf\n",
pTestElementIndex->ppIndexArray[i]->data);
}
}
int main() {
TestElement arr[] = {
{ "Test1", 5 },
{ "Test2", 8 },
{ "Test3", 4 },
{ "Test4", 9 },
{ "Test5", 1 },
{ "Test6", 2 },
{ "Test7", 0 },
{ "Test8", 7 },
{ "Test9", 3 },
{ "Test10", 6 }
};
unsigned Count = sizeof(arr) / sizeof(arr[0]);
TestElementIndex *pSorted = sortedIndexPointer(arr, Count, CompareNumbers);
DisplayElements(pSorted);
}