Passing array of strings to functions C
Solution 1
You can do it like this:
void sort(char **, int);
int main()
{
char *string_database[5]={'\0'};
string_database[0]="Florida";
string_database[1]="Oregon";
string_database[2]="California";
string_database[3]="Georgia";
sort(string_database, 4);
return 0;
}
void sort(char **str, int n)
{
int i = 0;
for (i = 0; i < n; i++)
printf("The string is= %s\n",str[i]);
}
Solution 2
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void sort(char *strings[], int n);//Function prototype
int main()
{
char *string_database[4]={'\0'};
string_database[0]="Florida";
string_database[1]="Oregon";
string_database[2]="California";
string_database[3]="Georgia";
sort(string_database, 4);
return 0;
}
void sort(char *strings[], int n)
{
int i;
for (i=0; i<n; i++) {
printf("String %d: %s\n", i, strings[i]);
}
}
You usually pass the length of the array along with the array itself. The char *strings[]
is really just sintactic sugar though, so if you want to keep the function prototype without parameter names you can use char **strings
as well, so that the code could be like this:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void sort(char **, int);//Function prototype
int main()
{
char *string_database[4]={'\0'};
string_database[0]="Florida";
string_database[1]="Oregon";
string_database[2]="California";
string_database[3]="Georgia";
sort(string_database, 4);
return 0;
}
void sort(char **strings, int n)
{
int i;
for (i=0; i<n; i++) {
printf("String %d: %s\n", i, strings[i]);
}
}
Also, as Jite below points out, using a syntax such as char *strings[]
might mislead you or another reader of the code into thinking they're dealing with a static matrix, while this is not true; you should therefore opt for the more straightforward char **strings
syntax.
Max twelve
Updated on November 28, 2021Comments
-
Max twelve over 2 years
i am currently confused as to how i can pass an array of strings to a function. I have created a one-dimensional array. The method that i have done works but it seems redundant and i think there is a better way of doing this yet i am unsure how. I am trying to find a way where i can pass all 4 elements to the function at one time.
Here is the sample of my code.
#include <stdio.h> #include <string.h> #include <ctype.h> void sort(char *,char *,char *, char *);//Function prototype int main() { char *string_database[4]={'\0'}; string_database[0]="Florida"; string_database[1]="Oregon"; string_database[2]="California"; string_database[3]="Georgia"; sort(string_database[0],string_database[1],string_database[2],string_database[3]); return 0; } void sort(char *string1, char *string2, char *string3, char *string4) { printf("The string is= %s\n",string1); printf("The string is= %s\n",string2); printf("The string is= %s\n",string3); printf("The string is= %s\n\n\n",string4); }
Thank you in advance, i appreciate any replies to my problem.
-
Eregrith almost 9 yearspossible duplicate of Passing multidimensional arrays as function arguments in C
-
Lundin almost 9 years@Eregrith This is not a multi-dimensional array, it is a one-dimensional pointer array.
-
-
Jite almost 9 yearsPlease add a length argument to the sort function (since you cannot do
sizeof(str)
in thesort()
function). -
Jite almost 9 yearsWell the answer is supposed to be useful to people wondering the same thing and doing it properly should be a high priority or atleast explain pitfalls etc.
-
Jite almost 9 yearsI would even say that using
*strings[]
as function argument is bad practice, in my opinion, it's easier to mistakenly dosizeof
on it (which is invalid). -
Eric almost 9 years@Jite The default main() is using
*argv[]
. It's programmer's task to usesizeof
correctly. -
Max twelve almost 9 years@Glaedr I don't understand the significance of two indirection operators when defining and calling the fucntion char ** strings The program does not work with one indirection operator.
-
Michele De Pascalis almost 9 years
strings
is a pointer to pointer to char: in C arrays are implemented as pointers to the first element when you pass them along functions it's the pointer that gets passed as a parameter.strings
is supposed to receive what in the main routine was an array of pointers to char, so it must be a pointer to the first element: a pointer to pointer to char. -
user over 2 yearsYes of-course goes without saying. Strings in C are character arrays. Arrays of string are 2-dimensional character arrays. Hence you can use a double subscript to get the value at the location in the matrix.