12

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.

2

3 Answers 3

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

}
Sign up to request clarification or add additional context in comments.

2 Comments

Please add a length argument to the sort function (since you cannot do sizeof(str) in the sort() function).
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.
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.

4 Comments

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 The default main() is using *argv[]. It's programmer's task to use sizeof correctly.
@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.
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.
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.