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;
}
The parameter array
of your function has an incorrect type.
In this call of printf
printf("%s\n", array[i]);
the argument array[i]
has the type char
. So you may not use the conversion specifier s
with an object of the type char
.
Also 0 is a valid index. So this return statement
return 0;
will confuse the caller of the function because it can mean that the string is found and at the same time that the string is not found.
The function can be declared and defined the following way
int findIndex( char **array, int n, const char *item )
{
int i = 0;
while ( i < n && strcmp( array[i], item ) != 0 ) i++;
return i;
}
though for indices and sizes of arrays it is much better to use the unsigned integer type size_t
instead of the type int
.
And in main the function can be called like
int pos = findIndex( items, index, some_string );
where some_string
is a string that should be searched in the array.
If the string is not found in the array then pos
will be equal to the current actual size of the array that is to index
,
So you can write for example in main after the call
if ( pos == index )
{
puts( "The string is not present in the array." );
}