'How to pass array of strings to a function in C
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.
Solution 1:[1]
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
Solution 2:[2]
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;
}
Solution 3:[3]
char *array to char (*array)[10]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | Divlaker |
