0

I can initialize a 2D array of strings using the following:

const char * const aListOfStrings[] {"test","test1"};

However, if I want to do the same thing when calling a function, such as:

void function( char const * const aListOfStrings[] ) {}

void CallWithArrayOfStrings()
{
    function( {"test", "test1"} );
}

The compiler politely refuses with the following error:

error: cannot convert '<brace-enclosed initializer list>' to 'char**'

I am able to pass a single string to a function as in:

void function( char const *someString ) {}

void CallWithString()
{
    function( "test" );
}

I seem to understand how to initialize arrays when they are variables, but apparently I don't seem to understand how it works in the context of calling functions. I have always assumed that initializing a variable is the same as initializing a function parameter? Have I been living a lie my whole life? ;)

Thank you

UPATE: Removed C tag. C arrays require an '=' sign before the brace initializer, and the error is different. It appears the problem is very similar however... Also fixed the closing quote.

1
  • 1
    const char * const aListOfStrings[] {"test","test1}; you missed double quotes at the end. Commented Dec 25, 2020 at 2:54

2 Answers 2

1

const char * const aListOfStrings[] {"test","test1"}; is a shorthand for

const char * const aListOfStrings[2] {"test","test1"};

Size is deduced.

But arguments of signature are fixed (unless template is used), moreover you cannot pass C-array by value, so:

void function(char const * const aListOfStrings[]) is equivalent to

void function(char const * const* aListOfStrings).

To have your syntax, you might use the (strange) syntax

void function(char const * const (&aListOfStrings)[2]);

Demo

or the template version

template <std::size_t N>
void function(char const * const (&aListOfStrings)[N]);

Demo

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

3 Comments

Thanks for taking time Jarod in answering this, but I need to keep the size deduction. Casting works (see my comment above).
Template version allows deduction (see link demo). With your casting, you don't have size information...
That makes sense. I did not think it through...the aListOfStrings[] as a parameter can't know how big it is at compile time, the only way is using a template. Thanks @Jarod42
1

That's because your compiler is not guessing the type of your inline argument, passed to function, so you must explicitly declare it before, eg:

const char* data[] = { "test", "test1" };
function(data);

But C++ has std::array, std::initalizer_list, std::vector, there's no reason to use an array of pointers to char unless you have a specific requirement.

1 Comment

I do have a specific requirement. Using a container isn't an option for this specific instance. Is it possible to cast it in some way? And just answered my own question: function2( ( char const * const []){"test","test2"} ); does the trick nicely.

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.