0

I have an array of strings and I would like to modify its elements at will. This is the code:

char pieces[9][4] = { "   ", " o ", " a ", "   ", "   ", "   ", " b ", "   ", "   " };
pieces[2] = { " x " };

As I know, the elements in pieces[] are string literal, so they can't be changed (I'm not sure why this is like this). Maybe it could be solved using std::string or vectors. However, I would like to know if this kind of operation, or very similar operations, can be done using an array of strings. Can be done something like this using just an array of strings?

2
  • The string literal can't be changed, but the a char[] initialized from a string literal certainly can be changed. Commented May 2, 2018 at 17:38
  • The elements are not string literals. They would be if it were declared char *pieces[9] = { ... };. But since you declared it char pieces[9][4], the elements of pieces are arrays, not pointers to literals. Commented May 2, 2018 at 17:40

5 Answers 5

4

In your specific situation, it looks like you always have some character surrounded by spaces, so you could simply do pieces[2][1] = 'x'; to modify that one element. However...

You are correct to assume this can be made easier with std::string and std::vector, but since we already know the size, an std::array will probably be better here:

std::array<std::string, 9> pieces = { "   ", " o ", " a ", "   ", "   ", "   ", " b ", "   ", "   " };
pieces[2] = " x ";

You may notice that the subscript operator still works on std::array's. This means that even if you switch to std::array's, you probably won't even have to change too much in your other code (just the parts dealing with c-strings to be dealing with std::strings)

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

2 Comments

To clarify: In this simple case there is practically no difference between std::array<std::string, 9> pieces and std::string pieces[9].
@HolyBlackCat I was going to add a part arguing the use of .at() over subscript for bounds checking, but left it off to keep the answer shorter. I think that would be the biggest reason I personally would prefer to be using an STL class over a raw array.
3

You can use strcpy();

See following example code. See working code here:

int main(void) 
{
    char pieces[9][4] = { "   ", " o ", " a ", "   ", "   ", "   ", " b ", "   ", "   " };

    printf("At point 1: %s\n",pieces[2]);
    strcpy(pieces[2]," x ");
    printf("At point 2: %s",pieces[2]);
    return 0;
}

Output:

At point 1:  a 
At point 2:  x 

Comments

1

Does

pieces[2][0] = ' ';
pieces[2][1] = 'x';
pieces[2][2] = ' ';
pieces[2][3] = '\0';

do what you want?

Comments

1

Firstly, using curly brackets like pieces[2] = { " x " }; is the way of initialization, so you can't do that.

Secondly, pieces[2] is an char array, so it is not modifiable l-value.

You can either change its content element by element or by using strcpy() function.

4 Comments

Curly braces can work for the right type. See here.
It seems that you are right even I have not encountered the right type till now. But the thing is char arr[3]; arr = {'a', 'b', 'c'}; is not working while char arr[] ={'a','b','c'}; is working.
If You try arr[4]?
No, it does not work too. But why did you think that it will work @RobertAndrzejuk ?
0

Just to sumarize the different solutions given by different users, the options are:

  • Modify one char element at a time. Example: pieces[2][1] = 'x'

  • Use strcpy(). Example: strcpy(pieces[2]," x ")

  • Another types: std::array, std::string, std::vector

Comments

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.