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]);
}
Answer from sps on Stack Overflow
Top answer
1 of 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]);
}
2 of 3
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);

}
Discussions

pointers - Passing array of strings as parameter in python ctypes - Stack Overflow
This is a followup to Multi-dimensional char array (array of strings) in python ctypes . I have a c function that manipulates an array of strings. The data type is static, so this helps: void cfu... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Passing array of strings to functions C - Stack Overflow
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: More on stackoverflow.com
๐ŸŒ stackoverflow.com
Passing a list of strings to from python/ctypes to C function expecting char ** - Stack Overflow
Anyway I have now changed the definition of the C function so it takes a length argument as the first argument. ... Sign up to request clarification or add additional context in comments. ... Notice that in Python 3.x, strings are unicode by default, so you should convert your strings to bytes ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to pass array of strings as function parameter?
Any array variable is really just a pointer to the first value of that array. The following values are stored in the next bits of memory according to the size of the type. For instance, char should be 1 byte so an array of char is just one char-per-byte in continuous memory. In general the way to pass arrays through functions is to pass the pointer to the array followed by the size of the array since C doesn't maintain any data about the allocated memory. void handleStrings(char* strings, int rows, int columns) { int i, j; for (i = 0; i < rows; i++) { for (j = 0; j < columns; j++) { // do something with strings[i][j] } } } More on reddit.com
๐ŸŒ r/C_Programming
6
0
December 4, 2016
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 88153-how-pass-array-strings-function-argument.html
How to pass an array of strings as a function argument?
April 2, 2007 - Just the same way as you would pass a pointer to another variable, just that in this case, you're passing a pointer to an array. What you want is the following: ... Please note that I've chosen to pass the number of options as an argument to the function, since myOptions is a pointer itself, sizeof(myOptions) would only return 4 which gives no indication about the number of elements. You can also wrap it all into one structure, like this: ... and then only pass the structure as parameter.
Top answer
1 of 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]);

}
2 of 3
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.

Find elsewhere
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 55229471 โ€บ c-programming-array-of-characters-or-string-as-function-parameter
C programming array of characters or string as function parameter - Stack Overflow
Except when it is the operand of the sizeof or unary & operators, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T", and its value will be the address of the first element of the array. So, if you passed string as an argument to a function, like so: ... In the context of a function parameter declaration (and only in that context), T a[N] and T a[] are both treated as T *a, so you could declare f to be any of
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ [c] how to pass an array of strings to a function
r/learnprogramming on Reddit: [C] How to pass an array of strings to a function
October 5, 2022 - #include <stdio.h> #include <string.h> void options_list(char *options[], int length){ for (int i = 0; i < length; i++) { printf("%d - %s", i, options[i]); } } int main() { char stringlist[3][20]; strcpy(stringlist[0], "Start"); strcpy(stringlist[1], "Options"); strcpy(stringlist[2], "Exit"); options_list(stringlist, 3); return 0; } I tried as argument : *options[] **options options[][] All leading to errors or SIGSEGV ยท Archived post. New comments cannot be posted and votes cannot be cast. Share ... This is what you're looking for. void options_list(char (*options)[20], int length){ for (int i = 0; i < length; i++) { printf("%d - %s\n", i, options[i]); } } This says that options is a pointer to an array of 20 chars.
๐ŸŒ
StudySmarter
studysmarter.co.uk โ€บ array as function argument in c
Array as function argument in c: 'Pass Array' | StudySmarter
August 10, 2023 - You can't determine the size of an array directly when it is passed as a function argument in C because it decays into a pointer. To determine the size, either pass the size as an additional parameter or use a special sentinel value to indicate the array's end.
๐ŸŒ
Quora
quora.com โ€บ How-do-I-pass-an-array-of-characters-as-a-parameter-to-a-function-C-language
How to pass an array of characters as a parameter to a function (C language) - Quora
Answer (1 of 10): AFAIK there are two ways, * if you know the size of array in advance, you can do something like this [code]void function(char array[10]) { } int main() { char array[] = {'a', 'b', ..., 'j'}; function(array) } [/code] * if not, you can do something like this [code]void fu...
๐ŸŒ
IncludeHelp
includehelp.com โ€บ c-programs โ€บ pass-an-array-of-strings-to-a-function.aspx
C program to pass an array of strings to a function
Strfun is the name of the function. char **ptr is the pointer to character pointer i.e. to store the address of a two dimensional character array (known as array of strings).
๐ŸŒ
Coddy.Tech
coddy.tech โ€บ learn โ€บ courses โ€บ functions in c โ€บ array as a parameter
Array as a Parameter โ€“ Functions in C | Coddy
When an array is passed to a function, the function receives a pointer to the first element of the array. ... #include <stdio.h> // Function definition void printArray(char *arr[]) { for (int i = 0; i <= 3; i++) { printf("%s ", arr[i]); } ...
Top answer
1 of 4
28

The warning is exactly right. Your function wants an array of pointers. You're giving it an array of arrays.

Expected:

 sep_foo:
 +------+       +-----+
 |char**|--> 0: |char*|-->"string1"
 +------+       +-----+
             1: |char*|-->"string2"
                +-----+
*sep_foo_qty-1: |...  |
                +-----+

What you provided:

           sep_foo:
           +--------------------------------+
        0: | char[MAX_STRING_LENGTH]        |
           +--------------------------------+
        1: | char[MAX_STRING_LENGTH]        |
           +--------------------------------+
MAX_QTY-1: | ...                            |
           +--------------------------------+

An array with elements of type X can "decay" into a pointer-to-X, or X*. But the value of X isn't allowed to change in that conversion. Only one decay operation is allowed. You'd need it to happen twice. In your case, X is array-of-MAX_STRING_LENGTH-chars. The function wants X to be pointer-to-char. Since those aren't the same, the compiler warns you. I'm a bit surprised it was just a warning since nothing good can come from what the compiler allowed to happen.

In your function, you could write this code:

char* y = NULL;
*sep_foo = y;

That's legal code since sep_foo is a char**, so *sep_foo is a char*, and so is y; you can assign them. But with what you tried to do, *sep_foo wouldn't really be a char*; it would be pointing to an array of char. Your code, in effect, would be attempting to do this:

char destination[MAX_STRING_LENGTH];
char* y = NULL;
destination = y;

You can't assign a pointer into an array, and so the compiler warns that the call is no good.

There are two ways to solve this:

  • Change the way you declare and allocate sep_foo on the calling side so it matches what the function expects to receive:

    char** sep_foo = calloc(MAX_QTY, sizeof(char*));
    for (int i = 0; i < MAX_QTY; ++i)
      sep_foo[i] = malloc(MAX_STRING_LENGTH);
    

    or, equivalently

    char* sep_foo[MAX_QTY];
    for (int i = 0; i < MAX_QTY; ++i)
      sep_foo[i] = malloc(MAX_STRING_LENGTH);
    
  • Change the prototype of the function to accept what you're really giving it:

    int parse(const char *foo, char sep_foo[MAX_QTY][MAX_STRING_LENGTH], int *sep_foo_qty);
    
2 of 4
15

Parameter 2 should be

char sep_foo[][MAX_STRING_LENGTH]

To clarify, you are passing a pointer to parse() and treating it as a pointer to a pointer. A multidimensional array in C is NOT an array of pointers. It is a single block of memory that is pointed to by the array variable. You cannot dereference it twice.

๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ c function arguments vs python function arguments.
r/C_Programming on Reddit: C function arguments vs Python function arguments.
April 21, 2021 -

In python, function arguments are treated like separate variables, a seperate entity in relation to the original object (From my understanding. I could be wrong). Is this different in C? Are function arguments in C equal to the original thing you put in as an argument? Meaning, if you input a string into a C function as a argument, will modifying that argument from the function modify the original?

๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ cprogramming โ€บ c_passing_arrays_to_functions.htm
Passing Arrays as Function Arguments in C
If you want to pass an array to a function, you can use either call by value or call by reference method. In call by value method, the argument to the function should be an initialized array, or an array of fixed size equal to the size of the ...