help with array of strings

Trying to get this code to work, know there are easier ways to do it but just testing out an array of strings...
I get the error message "incompatible types in assignment of 'char' to 'char [5]' on line 13. basically i want to initialize an array of strings using this method

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main()
{
    char str_list[5][5];
    char val = 'a';
    int i;

    for(i=0; i<5; i++)
    {
        str_list[i] = val;
        val++;
        cout << str_list[i] << endl;
    }

    return 0;
}

str_list is an array of arrays of chars. So what you are doing on line 12 is trying to assign a single char to an array of chars. To initialize such an array, you'll need two for loops; one to go over all the arrays, and another to go over all the chars of each of those arrays.
You have an array of arrays of char, not an array of strings.
This is a fixed version of your program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string str_list[5];
    string val = "a";

    for(int i=0; i<5; i++)
    {
        str_list[i] = val;
        val[0]++;
        cout << str_list[i] << endl;
    }
}
Each element of a two-dimensional array is selected by using two coordinates or indexes. So it is obvious that you did not specify two coordinates/indexes. And the compailer says you about this.

You should to write

1
2
3
4
5
6
7
8
for ( int i = 0; i < 5; i++ )
{
   for ( int j = 0; j < 5; j++ )
   {
        str_list[i][j] = val;
        cout << str_list[i][j] << endl;
   }
}
Last edited on
I'm still a little confused as the book I'm reading says to access an individual string you just specify the left index, which is what I thought I was doing with str_list[i]. I know I'm only assigning one character to each string so really it's an array of characters but that shouldn't matter...?
Oh, I see what you are saying. In that case, you'll want val to me a single character string, like this:
char val[] = "v";
However, you need to note that you cannot simply assign one C-string to another with =. You'll need allocate space in str_list[i] to hold the string contained in val[] then use strcpy() to assign it.
I've modified it to the following but still doesn't work...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char str_list[5][5];
    char val[2] = "a";
    int i,t;

    for(i=0; i<5; i++)
    {
        str_list[i] = strcpy(str_list, val);
        val[2]++;
        cout << str_list[i] << endl;
    }

    return 0;
}
Yes you are right, str_list[i] is one-dimensional character array. However 1) you can not assign a character to array; 2) arrays have no such operator as assignment/ To assign one array to another you should copy each element of source array. Usually for character arrays "assignement" of arrays is performed with using standard string functions declared in header file string.h in C or cstring in C++, as, for examplle, strcpy..
So in case of your example you could to write


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    char str_list[5][5];
    char val[2] = "a";
    int i;

    for(i=0; i<5; i++)
    {
        strcpy( str_list[i], val );
        val[0]++;
        cout << str_list[i] << endl;
    }

    return 0;
}
Last edited on
There are two problems -
First is that you cannot assign a single character value to an entire char[] array.
Second is that arrays and characters are completely different things which have completely different (and fairly incompatible) rules in how you're allowed to use them.

where char str_list[5][5] represents an array-of-arrays str_list[0] represents a complete array of 5 characters.

Since you can't assign a single character to an entire array, the C++ compiler doesn't understand what you're trying to do when you write str_list[0] = 'a';

Just to further compound your problems, arrays in C and C++ are not copyable using the = operator (This is partly for historical reasons).
- Therefore it would also be illegal to do this too: str_list[0] = "hi";

What I expect your book is alluding to is that you can use str_list[i] in order to pass a string around to various string-handling functions or objects such as cout. e.g.
1
2
3
4
for( int i = 0; i < 5; ++i )
{
    cout << str_list[i] << endl;
}

The reason this code works is that cout << recognises char arrays as a special case and knows how to step through each individual character to output the entire string without you needing to do that yourself - However this is a feature of the C++ iostream library, and not specifically a C++ 'language' feature


The short of it is that you need to jump through a few hoops in order to use arrays in code you write from scratch. This is one of the reasons that C++ replaces char arrays with its own string type - because the way strings are handled in the old C language is very unintuitive (and error-prone).

See the example above by Athar. You should have a much easier time using C++ strings, because you can treat C++ strings roughtly in the same way that you might treat an int or a char. (Strings are also resizable and know their own size - so no more "magic numbers")

If your book is telling you to use char arrays, then you may be suffering from an outdated book which had originally been written back in the 80s or 90s (C++ became a different language in 1998, and there are many books still around today which haven't been properly updated since then, so there's plenty of bad information/examples flying around unfortunately.)
Last edited on
Basically it talks about arrays of strings and how, to create an array of strings, you use a two-dimensional character array, with the left index determining the number of strings and the right index the maximum length of each string, including the null terminator.
So, for example:

char str_array[30][80]

would declare an array of 30 strings, each one holding a maximum of 79 characters and the null terminator. I recently discovered the ability to declare a specific string type (if you include <string>) but just wanted to learn about doing it this way...now i'm just completely confused
Topic archived. No new replies allowed.