structure object

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
using namespace std;
struct a {
char map[][3];
};
int main() {

}
a initialize(a B) {
    char a = '1';
B.map[3][3] = { {a,a++,a++},{a++,a++,a++},{a++,a++,a++} };
return B;
}


cannot convert '<brace-enclosed initializer list>' to 'char' in assignment

what does this error mean?
but all i did is to create an object of a and initialize the map on it
Eh, whatever works, works.
1
2
3
4
5
6
7
8
9
char array[3][3] = { {a,a++,a++},{a++,a++,a++},{a++,a++,a++} };
    
    for(int i = 0; i<3; i++)
    {
        for(int ii = 0; i<3; i++)
        {
            B.map[i][ii] = array[i][ii];
        }
    }
thank you. i appreciate it
but i want to know about the error
B.map[3][3] = { {a,a++,a++},{a++,a++,a++},{a++,a++,a++} }; B.map[2][2] references one item of type char of your array which is able to hold exactly one char.

B.map[3][3] does reference an item outside of your arrays range. Each row and each column are defined of size 3. But because C/C++ starts indexing arrays elements with 0, an index of 3 references the fourth, and thus not existing, element.

To assign values to every item of your array uses f.e. a loop as @poteto has shown.
1) { {a,a++,a++},{a++,a++,a++},{a++,a++,a++} }; is undefined See next post. (and even if it wasnt, there is an error)

2) If you are using C++, write C++. Prefer constructors or defalt value to initializer functions.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <numeric>

struct A {
    char map[3][3] = {{'1', '2', '3'},
                      {'4', '5', '6'},
                      {'7', '8', '9'}};
};

struct B {
    B()
    { std::iota(*map, *map + 9, '1'); }
    char map[3][3];
};

int main()
{
    A a;
    B b;
    std::cout << b.map[2][2] << '\n' << a.map[1][1] << '\n';
}


Last edited on
> { {a,a++,a++},{a++,a++,a++},{a++,a++,a++} }; is undefined.

It is not undefined; this is a braced-init-list.

Within the initializer-list of a braced-init-list, the initializer-clauses, including any that result from pack expansions, are evaluated in the order in which they appear. That is, every value computation and side effect associated with a given initializer-clause is sequenced before every value computation and side effect associated with any initializer-clause that follows it in the comma-separated list of the initializer-list.

[Note: This evaluation ordering holds regardless of the semantics of the initialization; for example, it applies when the elements of the initializer-list are interpreted as arguments of a constructor call, even though ordinarily there are no sequencing constraints on the arguments of a call — end note] - IS
@OP:

Maybe you want { {a++,a++,a++},{a++,a++,a++},{a++,a++,a} } instead of { {a,a++,a++},{a++,a++,a++},{a++,a++,a++} }?
@tcs
oh how stupid am i to think that the index of 3 is 3 not 4.
but that doesnt explain it but thank you because I thought that all this time 3 is 3 and not 4.

yea. im used to use looping and arrays together, i just make the error appeared. because
when im writing tictactoe i didnt have any choice but to find another way to avoid that error. then i realize i actually need that error too
@MiiNiPaayea thank you.
is there any way to assign the values by a new object of A?

i havent learned numeric library yet but thanks in advance i can make this my foundation when i come to that.
@JLBorges that was a good exp. and i think i understand half of it thank you so much
i havent learned numeric library
numerics is only needed for iota().
http://en.cppreference.com/w/cpp/algorithm/iota

It is pretty simple algorithm, assigning values in order. It is equivalent to:
1
2
3
char c = '1';
for(auto i = *map; i != *map + 9; ++i)
    *i = c++;
This uses some non-obvious properties of MD arrays. Or you can replace it with two loops like
poteto
suggested.

If you need to copy values from one object to other (what your initialize function does), this is job for assigment operator:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <algorithm>
struct A {
    char map[3][3] = {{'1', '2', '3'},
                      {'4', '5', '6'},
                      {'7', '8', '9'}};
    A& operator=(const A& other)
    {
        std::copy(*other.map, *other.map + 9, *this->map);
        //Alternative, nested loops:
        //for(int i = 0; i < 3; ++i)
        //    for(int k = 0; j < 3; ++j)
        //        this->map[i][j] = other.map[i][j];
        return *this;
    }
};

int main()
{
    A a;
    a.map[1][1] = '!';
    A b;
    std::cout << b.map[1][1] << '\n';
    b = a;
    std::cout << b.map[1][1] << '\n';
}
Last edited on
Topic archived. No new replies allowed.