I have a struct:
typedef struct DATA {
char *key;
char *parentKey;
char *description;
} DATA;
And an array of instances:
DATA *data_array = NULL; // the global data array
int m_arrayLength = 0; // Keeps track of the number of elements used
Once the array is populated I am sorting it using qsort
void SortData()
{
qsort(data_array, m_arrayLength, sizeof(DATA), CompareDataByKey);
}
int CompareDataByKey(const void *a, const void *b)
{
DATA *ia = (DATA *)a;
DATA *ib = (DATA *)b;
return strcmp(ia->key, ib->key);
}
And this is working as expected. I'm trying to implement a method that searches the array for a particular item and this is where I am stuck
DATA FindDataByKey(char *key)
{
DATA *searchData = malloc(sizeof(DATA));
searchData->key = key;
DATA result = bsearch(
searchData,
data_array,
m_arrayLength,
sizeof(DATA),
CompareDataByKey);
free(searchData);
return result;
}
The gcc
compiler is returning the message:
p_CONNECT.c: In function 'FindDataByKey':
p_CONNECT.c:87: error: invalid initializer
make: The error code from the last command is 1.
on line CompareDataByKey);
Can anyone explain the meaning of this error in the context of the code I have written?