11

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.

Max twelve
  • 131
  • 2
  • 2
  • 6
  • possible duplicate of [Passing multidimensional arrays as function arguments in C](http://stackoverflow.com/questions/4051/passing-multidimensional-arrays-as-function-arguments-in-c) – Eregrith Jun 23 '15 at 10:23
  • @Eregrith This is not a multi-dimensional array, it is a one-dimensional pointer array. – Lundin Jun 23 '15 at 10:38

3 Answers3

17

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]);

}
Arjun Mathew Dan
  • 5,240
  • 1
  • 16
  • 27
  • 4
    Please add a length argument to the sort function (since you _cannot_ do `sizeof(str)` in the `sort()` function). – Jite Jun 23 '15 at 10:20
  • 2
    Well 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 Jun 23 '15 at 10:22
5
#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.

Michele De Pascalis
  • 932
  • 2
  • 9
  • 26
  • I would even say that using `*strings[]` as function argument is bad practice, in my opinion, it's easier to mistakenly do `sizeof` on it (which is invalid). – Jite Jun 23 '15 at 10:27
  • @Jite The default main() is using `*argv[]`. It's programmer's task to use `sizeof` correctly. – Eric Jun 23 '15 at 10:46
  • @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. – Max twelve Jun 23 '15 at 22:25
  • `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. – Michele De Pascalis Jun 23 '15 at 23:40
0

after reading all the answer given for your question. I would like to tell you one more thing. you can also print your strings, character by character.

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++)
    {
       for (int j=0;str[i][j]!='\0';j++)
       {
          printf("%c",str[i][j]);
       }
       printf("\n");
    }
}

as str is a double-pointer, we can use two subscripts without any problem to get to characters.

Sahil
  • 13
  • 6
  • Yes 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. –  Oct 10 '21 at 20:02