Home > database >  How to pass array of strings to a function in C
How to pass array of strings to a function in C

Time:04-25

I am noob and I want to pass pointer of an array of strings to a function and that function simply prints the strings of the array using pointer arithmetic.

#include <stdio.h>
#include <string.h>

char words[][10] = { "madam", "Russia", "India", "Japan", "China", "Brazil", "Mexico" };
int size = sizeof(words) / sizeof(words[0]);

void printer(char *, int);

int main() {
    printer(*words, size);

    return 0;
}

void printer(char *array, int len) {
    for (int i = 0; i < len; i  ) {
        printf("%s\n", (array   i));
    }
}

Output:

madam
adam 
dam  
am   
m  

It is not printing elements (strings) one by one.

CodePudding user response:

As coded, printer takes a string and prints every final substring. You pass *words, which is the same as words[0], ie: the first string in the 2D array.

To pass a pointer to the array, the type of the argument should be char array[][10](*).:

#include <stdio.h>

char words[][10] = { "madam", "Russia", "India", "Japan", 
                     "China", "Brazil", "Mexico" };
int size = sizeof(words) / sizeof(words[0]);

void printer(char words[][10], int len);

int main() {
    printer(words, size);
    return 0;
}

void printer(char words[][10], int len) {
    for (int i = 0; i < len; i  ) {
        printf("%s\n", array[i]));
    }
}

Note however that you could also define array as an array of pointers to character strings char *array[]. This would allow for strings of any length, not limited to 9 characters plus a null terminator.

The code would be changed to this:

#include <stdio.h>

char *words[] = { "madam", "Russia", "India", "Japan", 
                  "China", "Brazil", "Mexico" };
int size = sizeof(words) / sizeof(words[0]);

void printer(char *words[], int len);

int main() {
    printer(words, size);
    return 0;
}

void printer(char *words[], int len) {
    for (int i = 0; i < len; i  ) {
        printf("%s\n", array[i]));
    }
}

The array words is initialized from string literals, which must not be changed, so it would be better to define it as const char *array[] and modify printer accordingly. Note also that printf("%s\n", array[i])) is equivalent to puts(array[i]) (good compilers detect this equivalence and generate the call to puts in both cases):

#include <stdio.h>

const char *words[] = { "madam", "Russia", "India", "Japan", 
                        "China", "Brazil", "Mexico" };
int size = sizeof(words) / sizeof(words[0]);

void printer(const char *words[], int len);

int main() {
    printer(words, size);
    return 0;
}

void printer(const char *words[], int len) {
    for (int i = 0; i < len; i  ) {
        puts(array[i]);
    }
}

(*) The function does not receive an actual 2D array as argument, but a pointer to the array of arrays, which technically has type char (*)[10] but the compiler will accept both syntaxes equivalently and char array[][10] is simpler and more readable

CodePudding user response:

You are just printing the 1st element of words[0], because you have passed words[0] which is equivalent to *words.

Also, to print the whole array of pointers, you need char [][10] as your parameter in function printer().

Note: You can put printer() function above main() function, then there will be no need for forward declaration.

Final Code

#include <stdio.h>

char words[][10] = {"madam", "Russia", "India", "Japan", "China", "Brazil", "Mexico"};
int size = sizeof(words) / sizeof(*words);

void printer(const char array[][10], int len){
    for(int i = 0; i < len; i  ){
        puts(array[i]);
    }
}

int main(void) {
    printer(words, size);
    return 0;
}

CodePudding user response:

char *array to char (*array)[10]

  • Related