6

I am having a hard time passing an array of strings to a function by reference.

  char* parameters[513]; 

Does this represent 513 strings? Here is how I initialized the first element:

 parameters[0] = "something";

Now, I need to pass 'parameters' to a function by reference so that the function can add more strings to it. How would the function header look and how would I use this variable inside the function?

1
  • The problem with that is that your "strings" remain uninitialized, so your second code snippet will corrupt your memory. Commented Mar 4, 2011 at 3:57

2 Answers 2

22

You've already got it.

#include <stdio.h>
static void func(char *p[])
{
    p[0] = "Hello";
    p[1] = "World";
}
int main(int argc, char *argv[])
{
    char *strings[2];
    func(strings);
    printf("%s %s\n", strings[0], strings[1]);
    return 0;
}

In C, when you pass an array to a function, the compiler turns the array into a pointer. (The array "decays" into a pointer.) The "func" above is exactly equivalent to:

static void func(char **p)
{
    p[0] = "Hello";
    p[1] = "World";
}

Since a pointer to the array is passed, when you modify the array, you are modifying the original array and not a copy.

You may want to read up on how pointers and arrays work in C. Unlike most languages, in C, pointers (references) and arrays are treated similarly in many ways. An array sometimes decays into a pointer, but only under very specific circumstances. For example, this does not work:

void func(char **p);
void other_func(void)
{
    char arr[5][3];
    func(arr); // does not work!
}
Sign up to request clarification or add additional context in comments.

3 Comments

Hi @Dietrich. Can you explain to me why the latter approach doesn't work. I'm working on a similar problem and I'm passing arr which is a [4][6] array containing 4 strings, 5 characters long to a func which takes char ** as argument. But I'm not able to retrieve the strings which I populated in callee function inside the called function func. Any help would be awesome!
You have to understand memory layout. Get a sheet of paper and draw things, you'll see that char **x and char y[4][6] are completely incompatible with each other. The compiler should be giving you error messages.
Remember that char ** is a pointer to char *. However, if you start with char y[4][6], there are no char * anywhere! So you can't have a pointer to char *.
2

char* parameters[513]; is an array of 513 pointers to char. It's equivalent to char *(parameters[513]).

A pointer to that thing is of type char *(*parameters)[513] (which is equivalent to char *((*parameters)[513])), so your function could look like:

void f( char *(*parameters)[513] );

Or, if you want a C++ reference:

void f( char *(&parameters)[513] );

2 Comments

That looks needlessly complicated. What's wrong with char *parameters[513]?
@Dietrich Epp: there's nothing wrong with it. How would you define a reference for such type? My answer is the answer to this question.

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.