I have an array of unknown size populated while reading the file in main function. I would like to write another function that iterates over this array, compare strings and return index of requested string.
However, I seem cannot iterate over all array and getting only the first element.
When trying to print an element found in the array (from findIndex), I get the following error: format specifies type 'char *' but the argument has type 'char' and I need to change to %c in the printf, as I understand this is because I'm iterating over the first item in the array, but not the whole array.
Is this because I'm creating an array in the main function as char *items[MAXKEY]? How can I fix the issue and return index of a requested string from a function?
int findIndex(int index, char *array, char *item) {
for (int i = 0; i < index; i++) {
if (strcmp(&array[i], item) == 0) {
printf("%s\n", array[i]); // rising an error format specifies type 'char *' but the argument has type 'char'
// return i; // does not return anything
}
}
return 0;
}
int main () {
FILE *file;
char *items[MAXKEY];
char token[MAXKEY];
int index = 0;
// adding elements to the array
while (fscanf(file, "%s", &token[0]) != EOF) {
items[index] = malloc(strlen(token) + 1);
strcpy(items[index], token);
index++;
}
return 0;
}