1

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;
}
8
  • 1
    // len is the length of the array. Don't describe it in words, show the actual code that defines and sets len. I bet it's not correct. Arrays passed to functions decay to pointers. sizeof will not work - are you using sizeof to get the len? Commented Jan 9, 2021 at 4:19
  • 2
    Also char request[] should be char *request[]. The compiler should have given you a warning about that. Always take notice of compiler warnings. Commented Jan 9, 2021 at 4:26
  • 2
    requests[i] there is no requests variable 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. Commented Jan 9, 2021 at 4:27
  • 2
    If you compile with gcc or clang, compile with -Wall -Wextra -Werror. Commented Jan 9, 2021 at 4:34
  • 2
    Then look at my second comment. There should be multiple warnings alerting you to problems in the code. Commented Jan 9, 2021 at 4:35

3 Answers 3

3

char request[] is not the same as char *request[MAXSTRING]. The former declares an array of characters (i.e. a string), the latter an array of pointers to char, i.e. an array of strings.

So declare it correctly in your function:

int findMatches (const char *filename, char *request[]) {

Next you will need a way to detect the end of the array of strings contained in request. Either pass a count to findMatches() or arrange for the last string to be NULL. If using a count you can redefine the function to accept a count:

void findMatches (const char *filename, char *request[], int n) {
    for (int i = 0; i < n; i++) {
        printf("%s \n", request[i]);
    }
}

And call it like this:

findMatches("filename.txt", request, agrc-1);

Also, the use of MAXSTRING in char *request[MAXSTRING] seems confused. You seem to want an array of strings, but MAXSTRING seems to be a maximum length of a string. It's unlikely that you will have 1000 arguments to your program.

Sign up to request clarification or add additional context in comments.

Comments

1

Passing arrays in C is the same as passing by pointer. You don't get the length, the function only sees it as a char *

You'll want to either pass the size or length in too, or wrap it in a struct and pass the struct in.

Comments

1

For passing arrays as argument in C you should pass as (array[], array_size) as it's treating it as just a pointer. So it will make easier for you to follow if you pass array_size too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.