Max twelve
Max twelve

Reputation: 141

Passing array of strings to functions C

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.

Upvotes: 12

Views: 43467

Answers (3)

Sahil
Sahil

Reputation: 13

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.

Upvotes: 0

Michele De Pascalis
Michele De Pascalis

Reputation: 952

#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.

Upvotes: 5

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5298

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

}

Upvotes: 17

Related Questions