4

I need to pass a pre-allocated array of strings as a function parameter, and strcpy() to each of the strings within the string array, as in this example:

 static void string_copy(char * pointer[]) {

    strcpy(pointer[0], "Hello ");

    strcpy(pointer[1], "world");

}

int main(int argc, const char * argv[]) {

    char my_array[10][100];

    string_copy(my_array);

    printf("%s%s\n", my_array[0], my_array[1]);

}

And the resulting printed string would be 'Hello world'.

How do I pass a pre-allocated string array and fill out each string within a function as shown above?

2
  • 2
    Use char** and a for loop Commented Aug 8, 2016 at 6:01
  • As in "static void string_array(char ** pointer[])"? That didn't compile. Commented Aug 8, 2016 at 6:06

3 Answers 3

6

When you are doing string_copy(my_array), you are passing a char (*)[100], i.e. pointer to char[100] array to your function. But your function is expecting a char *[], i.e. array of char pointers, because you have defined your function that way.

You can fix this by making changes so that your function (string_copy()) expects a char (*)[100], instead of a char *[].

For this, you can change your function definition as:

/* Your my_array gets converted to pointer to char[100]
   so, you need to change your function parameter
   from `char *pointer[]` to `char (*pointer)[100]` 
*/ 
/* static void string_copy(char *pointer []) */
static void string_copy(char (*pointer) [100])
{
    strcpy(pointer[0], "Hello ");
    strcpy(pointer[1], "world");
}

* Alternative Solution *

A different design/solution would be to change in your main() function so that you are actually passing a char *[], which decays into a char ** - which is fine - to string_copy(). This way you would NOT have to change your string_copy() function.

int main(int argc, const char * argv[]) {

    char my_array[10][100];
    int tot_char_arrs, i;
    char *char_arr_ptr[10];

    /* Get total number of char arrays in my_array */
    tot_char_arrs = sizeof(my_array) / sizeof(my_array[0]);

    // Store all char * 
    for (i = 0; i < tot_char_arrs; i++)
            char_arr_ptr[i] = my_array[i];

    /* Actually passing a char *[].
       it will decay into char **, which is fine
     */
    string_copy(char_arr_ptr);

    printf("%s%s\n", my_array[0], my_array[1]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Oh, I see. You have two versions, sorry.
@2501 I edited the post and highlighted that there are two versions, so that it will not confuse future readers. I think the way I had put it before could confuse people.
3

you need to use a pointer to the array. here is an example with 1 dimension array:

#include <stdio.h>
#include <stdbool.h>
#include <string.h>

static void string_copy(char **pointer) {

    strcpy(pointer[0], "Hello ");

}

int main(int argc, const char * argv[]) {

    char my_array[10];
    char * p_array = my_array;

    string_copy(&p_array);

    printf("%s\n",  my_array);

}

Comments

1

Your function can simply accept matrix dimensions and pass a const char * that stores the array of literals (pre-allocated) strings:

#include <stdio.h>
#include <string.h>

#define STRINGS_LENGTH 100

static void string_copy(size_t n, size_t m, char pointer[n][m], const char *strings_to_copy[])
{
    for (size_t i=0; i< n; i++)
    {
        strcpy(pointer[i], strings_to_copy[i]);
    }
}

int main( void )
{
    const char *strings[] = { "hello", "World" };
    char my_array[sizeof(strings)/sizeof(strings[0])][STRINGS_LENGTH];

    string_copy(sizeof(strings)/sizeof(strings[0]), STRINGS_LENGTH, my_array, strings);

    printf("%s %s\n", my_array[0], my_array[1]);
}

You can also change the structure of your code using dynamic allocation for your output array like:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

static bool string_copy(char *pointer[], const char *strings_to_copy[], size_t strings)
{

    for (size_t i=0; i< strings; i++)
    {
        pointer[i] = malloc(strlen(strings_to_copy[i])+1);

        if (pointer[i] != NULL)
            strcpy(pointer[i], strings_to_copy[i]);
        else
            return false;
    }

    return true;
}

int main(void)
{
    const char *strings[] = { "hello", "World" };
    char *my_array[sizeof(strings)/sizeof(strings[0])] = {0};

    if (string_copy(my_array, strings, sizeof(strings)/sizeof(strings[0])) )
    {
        printf("%s %s\n", my_array[0], my_array[1]);
    }

    for (size_t i = 0; i<sizeof(strings)/sizeof(strings[0]); i++)
        free (my_array[i]);
}

Comments

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.