Matt
Matt

Reputation: 503

Passing a String Array to a Function

I have a couple of string arrays in C++ that are defined as const strings

const string ABC[5] = {“1”,”2”,”3”,”4”,”5”};
const string DEF[3] = {“1”,”2”,”3”};

I need to pass each of them to the same function similar to this pseudo code:

void doSomething(string strArray[])
{
    // loop to access all strings in the array and do something .
};

But when called it gives me an error about type when it is passed to the function

doSomethign(ABC);
doSomethign(DEF);

This is the error generated

Error C2664 'void roster::strInput1(std::string [])': cannot convert argument 1 from 'const std::string [5]' to 'std::string []'

I have tried to use the const type as well but I am not sure what I am doing wrong.

Upvotes: 1

Views: 555

Answers (3)

JVApen
JVApen

Reputation: 11317

I disagree with the use of C-style arrays, I rather see std::array or std::vector. The 2 main issues with this are the loss of size-information and the decay to pointer behavior. That said, your underlying problem is const correctness.

For the purpose of simplicity, I'll rewrite your code to the equivalent based on std::vector next to the C-style variants. Please see the answer of P.W for why size information is important and how to deal with it. My answer is more focused on not introducing a templated method that requires you to expose the algorithm.

The following code defines a constant vector of strings. Once defined const, this should not change by the one receiving this object. (Ignoring mutable and const_cast).

const std::vector<std::string> ABC = {"1","2","3","4","5"};
const std::string ABC[] = {"1","2","3","4","5"};

As this object is created as const, it can never change and const_cast is undefined behavior. Mutable ain't used in std::string, so no internal state can change.

So what's happening with your function?

void doSomething(std::vector<std::string> &strArray);
void doSomething(std::string strArray[], size_t size);

This signature accepts a non-const vector of strings. Passing a const object to this function ain't allowed as this function signature allows modifications to the original instance.

So, how to fix: correct the signature to not allow any modifications. This is preferred if one doesn't modify its input arguments.

void doSomething(const std::vector<std::string> &strArray);
void doSomething(const std::string strArray[], size_t size);

Or if you need modifications, remove the const at creation:

 std::vector<std::string> ABC = {"1","2","3","4","5"};
 std::string ABC[] = {"1","2","3","4","5"};

As I've used std::vector instead of C-style arrays, we even get a third possibility that ain't possibles with the C-style arrays: pass by value.

void doSomething(std::vector<std::string> strArray)

In this case, the function gets a copy of the data. This could be useful if one has to do some manipulations to the vector without influencing the caller. For example: Convert all strings to upper case or sort the vector.

Upvotes: 1

P.W
P.W

Reputation: 26800

ABC and DEF when passed as parameters decay to a pointer of type const string* which is not compatible with the type of strArray[].

Moreover, if you want to loop through the arrays inside the doSomething function, you will also need to pass the size of the arrays so as to know the bounds of the arrays. Since the sizes of ABC and DEF are different, what you need to use here is a function template.

Here is an example to loop through an array of T and print the elements.

template<typename T, size_t N>
void doSomething(T (&arr)[N])
{
    // loop to access all elements in the array and print them.
    for(auto const& var: arr)
        cout << var << '\n';
}

See Demo here.

If it is just a std::string array, you can just parameterize over the non-type parameter N which is the size of the array. The function template in this case will just be:

template<size_t N>
void doSomething(const std::string (&arr)[N])
{
    // loop to access all strings in the array and do something .
    for(auto const& str: arr)
        cout << str << '\n';
}

Upvotes: 5

Vashdev Heerani
Vashdev Heerani

Reputation: 679

if you are passing const array try to write it in function too and put star instead of brackets.

void doSomething(const string *strArray)
{
// loop to access all strings in the array and do something .
};

Upvotes: -1

Related Questions