简体   繁体   中英

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. However, in that function, only 1 string remains and it looks like there isn't even an array. 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. Your current declaration of printWords only declares its parameter as a single dimensional array of characters, which explains why it's not working right.

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. 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.

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; 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 }; ). Then the argument type would be char **words , a bit like argv to main() when it takes arguments.

Note that standard C says main() returns an int . Using void is only valid on 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.)

Just change the function parameter to 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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