简体   繁体   English

将数组作为参数传递给 C 中的 Function

[英]Passing an Array as Argument to Function in C

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.我需要将数组参数从 main 传递给 function,但是我无法弄清楚为什么它只传递数组的第一个元素而不是预期的整个数组。 The values in the array are from argv.数组中的值来自 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;
}

char request[] is not the same as char *request[MAXSTRING] . char request[]char *request[MAXSTRING] The former declares an array of characters (ie a string), the latter an array of pointers to char, ie an array of strings.前者声明一个字符数组(即字符串),后者声明一个指向char的指针数组,即字符串数组。

So declare it correctly in your function:所以在你的 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 .接下来,您将需要一种方法来检测request中包含的字符串数组的结尾。 Either pass a count to findMatches() or arrange for the last string to be NULL.将计数传递给findMatches()或将最后一个字符串安排为 NULL。 If using a count you can redefine the function to accept a count:如果使用计数,您可以重新定义 function 以接受计数:

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.此外,在char *request[MAXSTRING]中使用MAXSTRING似乎很混乱。 You seem to want an array of strings, but MAXSTRING seems to be a maximum length of a string.您似乎想要一个字符串数组,但MAXSTRING似乎是字符串的最大长度。 It's unlikely that you will have 1000 arguments to your program.您的程序不太可能有 1000 个 arguments。

Passing arrays in C is the same as passing by pointer.在 C 中传递 arrays 与通过指针传递相同。 You don't get the length, the function only sees it as a char *您不知道长度,function 仅将其视为char *

You'll want to either pass the size or length in too, or wrap it in a struct and pass the struct in.您也需要传递大小或长度,或者将其包装在结构中并传递结构。

For passing arrays as argument in C you should pass as (array[], array_size) as it's treating it as just a pointer.为了将 arrays 作为 C 中的参数传递,您应该将其作为(array[], array_size)传递,因为它只是将其视为指针。 So it will make easier for you to follow if you pass array_size too.因此,如果您也传递array_size ,您将更容易理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM