简体   繁体   English

字符串数组和C函数的问题

[英]Problem with an array of strings and a function in C

I am just starting out with C and I am trying to make an array of 3 strings pass through a function. 我刚开始使用C,并且试图使3个字符串的数组通过一个函数。 However, in that function, only 1 string remains and it looks like there isn't even an array. 但是,在该函数中,仅剩下1个字符串,而且看起来甚至没有数组。 I have tried numerous things already but I can't seem to fix it. 我已经尝试了很多事情,但似乎无法解决。

#include <stdio.h>
#define NUM_WORDS 3
#define MAX_WORD_SIZE 64

void printStrings(char words[])
{
    //But now 'words' looks like this: "one"  
    for (int i = 0; i < NUM_WORDS; ++i)
        printf("%s", words[i]);
}

void main()
{
    char words[NUM_WORDS][MAX_WORD_SIZE] = { "one", "two", "three" };
    //At this point the array 'words' looks like this:  
    //{ "one", "two", "three" }
    printStrings(words);
}

Your declaration of words inside of main is correct, but to pass a two dimensional array to a function, you need to declare it as such in the function. 您在main内部声明的words是正确的,但是要将二维数组传递给函数,则需要在函数中声明它。 Your current declaration of printWords only declares its parameter as a single dimensional array of characters, which explains why it's not working right. 您当前的printWords声明仅将其参数声明为一维字符数组,这解释了为什么它无法正常工作。

The minimum required to declare this correctly is as follows: 正确声明此要求的最低要求如下:

void printStrings(char words[][MAX_WORD_SIZE])

However, in this case, you have the option of doing this: 但是,在这种情况下,您可以选择执行以下操作:

void printStrings(char words[NUM_WORDS][MAX_WORD_SIZE])

That has the advantage (and disadvantage) of implying a greater restriction on what can/should be handed to printWords. 这样做的优点(和缺点)意味着对可以/应该传递给printWords的内容有更大的限制。 It's somewhat akin to the difference between: 这有点像之间的区别:

void function(char foo[])

which takes an arbitrary sized array of characters, and 它需要一个任意大小的字符数组,并且

void function(char foo[FOO_SIZE])

which states that it's expecting an array of FOO_SIZE , no larger and no smaller. 指出期望的数组为FOO_SIZE ,该数组不会更大也不会更小。

Note that you can only ever elide the outermost (leftmost) dimension, the inner ones are required so the compiler knows the ultimate size of each outer element. 请注意,您只能移出最外部(最左侧)的尺寸,而内部尺寸是必需的,因此编译器知道每个外部元素的最终尺寸。

You need: 你需要:

#include <stdio.h>
#define NUM_WORDS 3
#define MAX_WORD_SIZE 64

static void printStrings(char words[][MAX_WORD_SIZE])
{
    for (int i = 0; i < NUM_WORDS; ++i)
        printf("%s\n", words[i]);
}

int main(void)
{
    char words[NUM_WORDS][MAX_WORD_SIZE] = { "one", "two", "three" };
    printStrings(words);
    return 0;
}

You have a full 2D array; 您拥有完整的2D阵列; you must specify the size of all dimensions except the first, as shown. 您必须指定除第一个尺寸外的所有尺寸的尺寸,如图所示。 This is completely different from an array of pointers ( char *words[] = { "four", "five", "six", NULL }; ). 这与指针数组完全不同( char *words[] = { "four", "five", "six", NULL }; )。 Then the argument type would be char **words , a bit like argv to main() when it takes arguments. 那么参数类型将是char **words ,当它接受参数时有点像argvmain()

Note that standard C says main() returns an int . 请注意,标准C表示main()返回一个int Using void is only valid on Windows; 使用void仅在Windows上有效。 everywhere else, it is wrong. 在其他任何地方,这都是错误的。

(I use static because the function isn't going to be referenced outside this source file. Many (most) people don't bother with that. I use compiler options to make it necessary.) (我使用static是因为不会在该源文件之外引用该函数。很多(大多数)人都不会为此而烦恼。我使用编译器选项来使其成为必需。)

Just change the function parameter to char char words[][MAX_WORD_SIZE] : 只需将函数参数更改为char char words[][MAX_WORD_SIZE]

#include <stdio.h>
#define NUM_WORDS 3
#define MAX_WORD_SIZE 64

void printStrings(char words[][MAX_WORD_SIZE])
{
    //But now 'words' looks like this: "one"  
    for (int i = 0; i < NUM_WORDS; ++i)
        printf("%s", words[i]);
}

void main()
{
    char words[NUM_WORDS][MAX_WORD_SIZE] = { "one", "two", "three" };
    //At this point the array 'words' looks like this:  
    //{ "one", "two", "three" }
    printStrings(words);
}

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

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