Reputation: 3
I have a program that currently reads in words to a variable *char wordlist[lengthOfArray]; So basically it holds char word[10]. I can calculate average word lengths successfully, but I have a method that I want to print the words longer than this average recursively
void printAverage(char *wordlist, int n, int average){
if (n > 0){
if (strlen(wordlist[0]) > average){
printf("%s\n", wordlist[0]); // Print the one longer than average then move on
printAverage(wordlist + 1, n-1, average);
}
else {
printAverage(wordlist+1, n-1, average);
}
}
}
I have looked for hours online to see what is wrong, but for some reason, the comparison if (strlen(a[0]) > average) isn't getting the value of my word correctly, and giving me the error passing arg 1 of strlen makes pointer from Integer without a cast.
Does anyone have any idea how exactly I can do this correctly? Thanks in advance for any help, I am just stuck and I have already tried many things to no avail.
Upvotes: 0
Views: 118
Reputation: 1757
You are declaring wordlist as a pointer to char instead of pointer to pointer to char. char is an integral type so when you dereference it as wordlist[0] and pass it to strlen() the compiler warns you about passing an integral type instead of a pointer.
Try:
void printAverage(char **wordlist, int n, int average)
{
if (n > 0){
if (strlen(wordlist[0]) > average){
printf("%s\n", wordlist[0]); // Print the one longer than average then move on
}
printAverage(wordlist + 1, n-1, average);
}
}
Upvotes: 0
Reputation: 63471
Your variable wordlist
is not an array of words - it is a string. If you have an array of words it's either a 2D array like this:
char *wordlist[]
Or this:
char wordlist[][10]
Or a double-pointer like this:
char **wordlist
You need to set up the parameter according to the actual data you are passing into it.
Upvotes: 1