Passing char array by reference

You can pass a pointer by reference. To do this you need the following syntax:

void func(char *&array)
{
    // ....
}

Inside the function you use this parameter as a simple pointer. If the value is modified, these changes are visible outside.


What you can probably do is:

void func( char (& array)[10] ) {

}

Which translates to: pass an array ([..]) of 10 ( [10] ) characters ( char ) by reference ( (& ..) ).


You're not passing the array by reference (nor should you, it will do you no good here). You are passing a pointer to its first element. You then reassign that pointer to point to something else inside the function. This has no effect on the array. If you want to change the contents of the array, then you need to copy data to the place that the pointer points to. You can use strcpy or similar for that:

strcpy(array, "Inserting data in array a");

As a side comment, but a very important one. We don't need to deal with things like this in C++ anymore. That's how you do things in C. Here's how we do things in C++:

#include <string>
#include <iostream>

void func(std::string & str)
{
    str = "Inserting data into the string";
    std::cout << str << std::endl;
}

int main()
{
    std::string a;
    func(a);
    std::cout << a << std::endl;
}