I need to pass an array argument to a function from main, however I cannot figure out why does it pass down only the first element of the array and not the whole array as expected. The values in the array are from argv.
Can someone please point out my mistake?
#define MAXSTRING 1000;
int findMatches (const char *filename, char request[]) {
// iterating over request[] is giving individual characters of the first word, not all words.
// expected to have a full array of input words.
int len = strlen(request);
for (int i = 0; i < len; i++) {
printf("%s \n", request[i]);
}
}
int main (int agrc, char *argv[]) {
char *request[MAXSTRING];
int index = 0;
for (int i = 1; i < agrc; i++) {
request[index] = argv[i];
index++;
}
findMatches("filename.txt", request);
return 0;
}
// len is the length of the array. Don't describe it in words, show the actual code that defines and setslen. I bet it's not correct. Arrays passed to functions decay to pointers.sizeofwill not work - are you usingsizeofto get thelen?char request[]should bechar *request[]. The compiler should have given you a warning about that. Always take notice of compiler warnings.requests[i]there is norequestsvariable defined and this would not have compiled. Clearly you are not showing actual code that reproduces the problem. Next time please be sure to post real code as a complete minimal verifiable example.-Wall -Wextra -Werror.