-1

I've been searching for hours how to find a way to pass strings to a function in C but they all seem to need either the length of the array or very specific information. I'm trying to make a function that will list a given array of strings so can vary in lengths in either dimension.

Currently I have:

void printStrings(char *arr[])
{
    for (int i = 0; i < 2; i++)
    { //print all list items
        printf("   %s\n", arr[i]);
    }
}

int main(int argc, char *argv[])
{
    char *list[2] = {"Hello", "Goodbye"};
    printStrings(list);
    return 0;
}

I want to be able to read the length of the array in printStrings to eliminate the hard coded 2 in the for loop so that it can run for all lengths.

This is not the same as asking the length of an array directly as it involves passing the array to a function.

3
  • 1
    What's wrong with passing down the length alongside the array? Commented Mar 23, 2019 at 14:27
  • @n.m. I was planning to make a header file to re-use the code so I could simply pass the array list for any future projects so wanted it with as little extra on the front end as possible (front end being the printStrings function). Commented Mar 23, 2019 at 14:38
  • 1
    There are exactly two ways to determine size of a pointer array passed to a function in C. Pass the size separately, or null terminate the array. If you don't like them both, you need a different language. Commented Mar 23, 2019 at 15:00

3 Answers 3

2

you can use struct for this.

struct string_list{
   char **list;
   size_t len;
};

void printStrings(struct string_list s_list)
{
    for (int i = 0; i < s_list.len; i++)
    { //print all list items
        printf("   %s\n", s_list.list[i]);
    }
}

int main(int argc, char *argv[])
{
    struct string_list s_list;
    char *list[2] = {"Hello", "Goodbye"};
    s_list.list = list;
    s_list.len = sizeof(list) / sizeof(list[0]);
    printStrings(s_list);

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

Comments

1

If you want to stick to char* arrays, you don't. If you use C strings, they are 0 terminated and their length can be gotten with using strlen().

You can, however, do something like:

struct string {
    char* String;
    int   Length;
};

And pass in an array of structs. Obviously you have to correctly assign the pointer to the correct strings and store the correct length of each.

2 Comments

I don't mind if it's pointers or passing the array but I can't seem to get it working without either
@hamishsams you can't, in C you have to pass the length of the array or the array hast to have some kind of marker, for example a NULL pointer.
1

Terminate the array of pointers with NULL. Then iterate until the NULL is found.

#include <stdio.h>

void printStrings(char *arr[])
{
    int i = 0;
    while ( arr[i])//not NULL
    { //print all list items
        printf("   %s\n", arr[i]);
        i++;
    }
}

int main(int argc, char *argv[])
{
    char *list[] = {"Hello", "Goodbye", NULL};
    printStrings(list);
    return 0;
}

1 Comment

Is there any way to do this with a function that adds null to the end of an array?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.